# Push notification trouble

**URL:** <https://forum.ionicframework.com/t/push-notification-trouble/218578>\
**Category:** Ionic Vue\
**Created:** [December 20, 2021, 5:59am UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578 "2021-12-20T05:59:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 20, 2021, 5:59am UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/1 "2021-12-20T05:59:11Z")

</div>

I have a Ionic Vue app that I want to get push notifications working on. So far I have iOS working fine and I get a FCM token and have successfully targeted notifications.

Android on the other hand doesn’t work. I can get the FCM token but when I try to push to it the notification never comes in. So I think I’m close. What am I missing?

UPDATE: Checking console.log and it says:

```auto
FirebaseMessagingError: The registration token is not a valid FCM registration token

{
code: 'messaging/invalid-argument',
message: 'The registration token is not a valid FCM registration token'
},
codePrefix: 'messaging'
}

```

But, but… that comes from this line!

```auto
await PushNotifications.requestPermissions();
await PushNotifications.register();
const pushToken = await FCM.getToken();

```

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [December 20, 2021, 9:52pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/2 "2021-12-20T21:52:45Z")

</div>

Are you using the official [Push Notification](https://capacitorjs.com/docs/apis/push-notifications) plugin?

If so, you should be using the listener.

```auto
await PushNotifications.addListener('registration',
    (token: Token) => {
        // Do stuff with token.value
    }
)

```

[This](https://capacitorjs.com/docs/guides/push-notifications-firebase) is also a great guide if you haven’t seen it yet.

---

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 20, 2021, 10:11pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/3 "2021-12-20T22:11:35Z")

</div>

I’ve very confused between the Push Notification plugin and the FCM plugin. All I know is I want native push notifications through the Firebase messaging system so I figured getting the FCM token was what I want with `FCM.getToken()` vs that which seems like the what? APN?

---

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 20, 2021, 10:16pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/4 "2021-12-20T22:16:57Z")

</div>

Ok I switched from `FCM.getToken()` to the code you suggested above. That results in me getting a token which seems valid when I try to send via Firebase messaging but my app crashes! lol

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [December 20, 2021, 10:17pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/5 "2021-12-20T22:17:24Z")

</div>

In my project I am just using the Push Notification plugin. The listener gives you the FCM token.

For iOS, you do need to edit your `AppDelegate.swift` file adding the following code. Otherwise by default the listener gives you the APN instead of the FCM token which for me didn’t work (Firebase didn’t recognize the APN).

```auto
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
    Messaging.messaging().token(completion: { (token, error) in
        if let error = error {
            NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
        } else if let token = token {
            NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token)
        }
    })
}

```

This is referenced [here](https://capacitorjs.com/docs/guides/push-notifications-firebase#add-initialization-code).

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [December 20, 2021, 10:18pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/6 "2021-12-20T22:18:58Z")

</div>

Not sure on the crashing…lol. I’d be glad to share my code if need be. I am also using Vue.

---

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 20, 2021, 10:19pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/7 "2021-12-20T22:19:01Z")

</div>

iOS isn’t an issue for me as I’ve had that working for weeks now (without modification of `AppDelegate.swift` either). I am just trying to get Android working.

---

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 20, 2021, 10:39pm UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/8 "2021-12-20T22:39:22Z")

</div>

If you don’t mind sharing the bits that matter. It’s funny, I am getting messages from Firebase Functions that the message was sent:

```auto
Successfully sent message: projects/jetfoil-mobile-dev/messages/0:1640039314900193%72cb085572cb0855

```

vs this:

```auto
FirebaseMessagingError: The registration token is not a valid FCM registration token

```

So I feel like I’m on the right track. And if the app is running and I wait a minute and then send a test push the app crashes immediately. So I know it’s TRYING to handle the incoming push. Funny thing is I didn’t do any `pushNotificationReceived` or `pushNotificationActionPerformed` listeners yet! This is my code now:

```auto
PushNotifications.requestPermissions().then(result => {
     if (result.receive === 'granted') {
          PushNotifications.register();
     }
});

await PushNotifications.addListener('registration', (token: any) => {
     setPushToken(token.value);
});

```

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [December 21, 2021, 1:50am UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/9 "2021-12-21T01:50:42Z")

</div>

On the simplest level, that is what I am doing. I add the listeners in my main.ts/js file before `createApp`.

Not sure what would cause it to crash. Might need to look at the errors in Android Studio. Assuming you’ve added the `google-services.json` auth file. You also don’t need to add any dependencies to the Android project as Capacitor automatically includes `firebase-messaging`.

---

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 21, 2021, 4:18am UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/10 "2021-12-21T04:18:27Z")

</div>

I put my code in the App.vue file as main.ts fires before I have connectivity to Firebase which is where I store their token. Do you store the token in the db? Perhaps I’m just doing it all wrong.

---

<div class="post-metadata">

**Author:** ![tkclark](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tkclark/32/144585_2.png) [@tkclark](https://forum.ionicframework.com/u/tkclark)\
**Post date:** [December 21, 2021, 6:02am UTC](https://forum.ionicframework.com/t/push-notification-trouble/218578/11 "2021-12-21T06:02:29Z")

</div>

Ok I go it working by removing the `android` folder, re-creating it and comparing within git to make sure my customizations were still intact. Redeployed and it worked!
