Home > Software design >  Registering for capacitor Push Notifications
Registering for capacitor Push Notifications

Time:09-05

I am trying to set up Push Notifications for my React Ionic project via Firebase. I have done everything based on the documentation and I am able to receive push notifications both in foreground/background and in both IoS & Android.

Below is the snippet I use to register:

PushNotifications.requestPermissions().then(result => {
            console.log(result)
            if (result.receive === 'granted') {
                // Register with Apple / Google to receive push via APNS/FCM
                PushNotifications.register();
            } else {
                // Show some error
            }
        });

My question is; where should this code live?

  • Does it need to be on my App component and thus being called every singe time it is re-rendering?
  • Does it need to be in the index.js component and thus (I assume) only runs when the app is reloading?
  • Do I need to register once and then keep checking if the device is still registered? If yes, how do I do so?
  • Something else I am missing?

I have the feeling that I am missing something obvious here, but I cannot pinpoint what. Everything works fine, I just want to make sure I have a good understanding on how this is supposed to work in order to get it right.

Thanks in advance for any responses, I am just trying to get my head around how things work/should work and the documentation isn't doing the trick.

P.S. I would love if someone could also explain the differences/benefits between capacitor/push-notifications and ionic-native/fcm. P.S2 Any kind of resources that explain these things in detail would be very welcome.

CodePudding user response:

PushNotifications.requestPermissions() is the function that opens the iOS system prompt that asks the user "Do you want to receive notifications from this app?"

For this reason, you must carefully choose when to call it.

If you put it in index.ts, for example, then the user will be immediately prompted to enable push notifications as soon as they open your app for the first time. Many users do not want to give permission to send notifications as soon as they install an app (they want to check out the app first), so if you do it this way, you will probably have more users block your notifications.

If your app has an onboarding (getting started) flow, one normal pattern is to have a step in onboarding that lists the benefits of getting notifications from your app and then provides a "next" link which calls PushNotifications.requestPermissions(), which will prompt the user to enable notifications.

Note that you can only ever ask users on iOS ONCE to enable notifications. So no matter how many times you call this function, your app only gets one chance! That's why it is so important that you call it at a timing when the user is likely to say yes.

At present, none of this is relevant to Android because Android enables notifications for apps by default.

  • Related