react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

iOS: onRegister only called with requestPermissions: true

Open drbarto opened this issue 5 years ago • 18 comments

Hey,

I would like to run PushNotification.configure on app start without immediately requesting permissions from the user, therefore I set requestPermissions to false. However, in this case onRegister never gets called. (Neither do I get an error via PushNotificationIOS.addEventListener). When I set requestPermissions to true, onRegister is called with the token correctly.

So... 1) is this by design? and 2) is there a way to get the token without requesting permissions?

(running on an iPhone XR -- real device -- with iOS 12.2; the push entitlement is set correctly in Xcode)

drbarto avatar Apr 18 '19 20:04 drbarto

I can't seem to get onRegister to trigger at all.

calebpanza avatar Jul 16 '19 21:07 calebpanza

Same. Triggered just fine on Android, but never called on iOS 10.x+. Works fine on iOS 9.x. 🤔

markpar avatar Jul 30 '19 14:07 markpar

I am running into this same issue. Has anyone found a cause, or better yet, a solution?

nolanrbrady avatar Aug 30 '19 17:08 nolanrbrady

Have you enabled "Push Notifications" in Xcode - Capabilities for the application?

codeoholic avatar Sep 27 '19 11:09 codeoholic

I'm having the same problem after updating from iOS 9 to iOS 11, react-native 0.57.1 to 0.61.2 and react-native-push-notification from 3.0.2 to 3.1.9

I enabled Push Notifications in Xcode - Capabilities for the application and also Remote Notifications on the Background Modes but it still doesn't fire the onRegister event, even after the user has Allowed notifications from the default prompt. The push notification is also enabled in the Apple developer account, since this worked in a previous version that is already published on the app store.

Here is part of my code executed on the component:

componentDidMount() {
    PushNotification.configure({
      onRegister: token => {
        AsyncStorage.setItem('token', token.token);
      },
      onNotification: notification => {
        ...
      },
      senderID: 'XXXXXXXX',
      popInitialNotification: true,
      permissions: { alert: true, badge: true, sound: true },
      requestPermissions: true
    });
  }

Am I missing something after doing this updates?

CGReinhold avatar Oct 23 '19 19:10 CGReinhold

This is happening for me as well, just nothing for onRegister.

I've tried putting it into state, asking later,

I've tried using my own Event delegator to get to it outside of the react scope

I've tried requestPermissions:true

Push notifications cert not expired

Push Notifications capability enabled in Xcode

Last time I updated the framework this happened as well, this part seems quite buggy...

atebit avatar Feb 13 '20 14:02 atebit

Also, there's no error.. Since, the code is in Pods, is there a way to log what's happening? I see the code in the Pod, but I am not going to edit it...

atebit avatar Feb 13 '20 14:02 atebit

Nevermind, I had forgotten to update the AppDelegate.m:

https://github.com/react-native-community/react-native-push-notification-ios

atebit avatar Feb 13 '20 15:02 atebit

@atebit are you using RN version >0.60? If yes, why would you need to edit AppDelegate.m?

The problem seems to appear again: https://github.com/react-native-community/push-notification-ios/issues/106

schumannd avatar Apr 28 '20 13:04 schumannd

@schumannd Even if you have RN version > 0.60, you still have to edit AppDelegate.m, auto-linking is going to edit it for you, it will just link project pods automatically. The changes reflected in AppDelegate.m forward notifications events to this module.

magrinj avatar Sep 10 '20 14:09 magrinj

I have enabled push notifications and remote notifications. I have updated both .m and .h files as instructed (twice just incase). Changed the import format as well. Am I missing something here? Screen Shot 2021-02-10 at 9 06 26 PM

my-name-is-nheo avatar Feb 11 '21 02:02 my-name-is-nheo

Anyone got this? onRegister never called if requestPermissions = false.

dzpt avatar Aug 31 '21 17:08 dzpt

@dzpt Did you call PushNotificationsHandler.requestPermissions() to trigger onRegister() on any other screen?

codeoholic avatar Aug 31 '21 17:08 codeoholic

@codeoholic Yes, i want to manually requestPermission, so i call it on the first time user open the app. onRegister is called on that time

after i re-open the app, onRegister is never be called again

dzpt avatar Aug 31 '21 17:08 dzpt

@dzpt onRegister gets called only once during the lifescycle. Try reinstalling the app and mange it wisely.

Here's how I manage it.

//App.js
PushNotification.configure({
    onRegister: function (token) {
        console.log("TOKEN:", token);
        userNotificationToken( navigationRef, token );
    },
    onRegistrationError: function(err) {
        console.log( err );
    },
    onNotification: function (notification) {
        console.log("NOTIFICATION:", notification);
        notification.finish(PushNotificationIOS.FetchResult.NoData);
    },
    permissions: {
        alert: true,
        badge: true,
        sound: true,
    },
    popInitialNotification: false,
    requestPermissions: false
});
//NotificationRequest.js
const triggerNotificationsRequest = () => {
    PushNotification.requestPermissions();
}

triggerNotificationsRequest will call onRegister in App.js which is higher up the order.

codeoholic avatar Aug 31 '21 17:08 codeoholic

@codeoholic i reinstall the app also, but it didn't work, i call to requestPermissions() once on the first launch only. and onRegister is called on that time only.

onRegister is supposed to be called everytime user re-open the app. do you call triggerNotificationsRequest everytime open the app?

dzpt avatar Aug 31 '21 17:08 dzpt

@dzpt Crisp answer, NO. Reason : iOS guidelines only allow one time prompt for notification permission. If I try to do so, it will be blocked at OS level. So, when I prompt user for notification for the first time, I store the token in AsyncStorage. On next load, I check Async first to check if I have to show user the NotificationRequest screen or not.

codeoholic avatar Aug 31 '21 17:08 codeoholic

@codeoholic so the error is persisted. Because if you set requestPermissions=true, onRegister / onRegistrationError will be called everytime open the app.

storing token might give the incorrect value, for example if user manually turn off the notification on the settings. onRegistrationError must be triggered, you would waste the resource to send to disabled devices

dzpt avatar Aug 31 '21 18:08 dzpt