Ghonche Yqr
Ghonche Yqr

Reputation: 374

Issue with receiving push notifications on iOS using Capacitor and Firebase

I am facing a problem with receiving push notifications on iOS using Capacitor and Firebase Cloud Messaging. Here are the details:

Despite these steps, notifications are not received on iOS devices/emulators. I would appreciate any guidance. Thank you in advance.

Upvotes: 1

Views: 4200

Answers (3)

amcastror
amcastror

Reputation: 568

In my case I don't use FCM and despite following @mleister 's answer, I couldn't get the token in the correct format.

What I ended up doing was transforming the token into a simple string with:

let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()

So the final code in the AppDelegate.swift is:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
    NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}

This gave me the old fashion format that I can use in AWS SNS platform.

The code for transforming de Data into string was taken from this post.

Upvotes: 0

mleister
mleister

Reputation: 2585

I encountered the same issue. I simply followed this guide: https://capacitorjs.com/docs/apis/push-notifications. However, the problem is that many steps are missing from this guide. You don't need to replace the npm package @capacitor/push-notifications as suggested in the accepted answer.

In addition, follow this guide and the problem will be resolved: https://capacitorjs.com/docs/guides/push-notifications-firebase#prerequisites


Background: Without the Firebase initialization code mentioned in the prerequisites, the push token has an incorrect format.

Without the prerequisites, the push token you receive from the looks like this: 3B7CA1BCAE6B0C972CCE9D9D10B98E88160185A8D65C08294F4EFBA509D5E539

After completing all the steps successfully, the token has a different format with colons and dashes, and it becomes significantly longer. I hope this helps!

Token format after including the prerequisites: fAs7FPRhTGqH9gXMYX3vHy:APA81bF9gCc99u-Q2IuylC9Nctn1nAmlVgCu3U8dCvSHvWIX3MHFG1t5BoXFS6CCkSETiB9iqxz-CgyNtfn2DJYFF3vod_puQOW2xsh-a_I7XfURFAJqSmrQDBo9RCc55XX-3N47Bq54

Upvotes: 0

Ghonche Yqr
Ghonche Yqr

Reputation: 374

I used @capacitor-firebase/messaging plugin instead of @capacitor/push-notifications and that worked for me.

( This guide helped me ,Thanks to RGe )

Upvotes: 3

Related Questions