ios icon indicating copy to clipboard operation
ios copied to clipboard

PushNotificationIOS.addEventListener('register' does not return apns token

Open arnoldas500 opened this issue 4 years ago • 14 comments

Bug

Environment info

react-native info output:

info React Native Environment Info: System: OS: macOS 10.15.2 CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz Memory: 749.49 MB / 32.00 GB Shell: 5.7.1 - /bin/zsh Binaries: Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1 Android SDK: API Levels: 21, 22, 23, 25, 26, 27, 28 Build Tools: 22.0.1, 25.0.0, 26.0.1, 26.0.2, 28.0.3, 29.0.0 System Images: android-21 | Google APIs Intel x86 Atom, android-25 | Google Play Intel x86 Atom, android-26 | Google APIs Intel x86 Atom IDEs: Android Studio: 3.5 AI-191.8026.42.35.5791312 Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild npmPackages: react: 16.8.3 => 16.8.3 react-native: https://github.com/expo/react-native/archive/sdk-34.0.1.tar.gz => 0.59.8 npmGlobalPackages: react-native-cli: 2.0.1 react-native-git-upgrade: 0.2.7 react-native-log-ios: 1.0.1 react-native-vector-icons: 6.6.0

Library version: 1.1.1

Steps To Reproduce

  1. Follow steps to install and link push-notificaiton-ios
  2. insert reproducible sample code below into app.js componentDidMount

Describe what you expected to happen:

1.App loads and ask for push notification permissions (this works) 2.Console logs the device token (does not work and no token is printed)

Reproducible sample code

async componentDidMount() { console.log('component did mount PushNotificationHandler') PushNotificationIOS.addEventListener('register', token => { console.log("***** PushNotificationHandler CALLLEDDD ********************* ", token) Alert.alert(token) })

    PushNotificationIOS.addEventListener('registrationError', registrationError => {
        console.log(registrationError, '-- *********************')
    })

    PushNotificationIOS.addEventListener('notification', function(notification) {
        if (!notification) {
            return
        }
        const data = notification.getData()
        Alert.alert(JSON.stringify({ data, source: 'CollapsedApp' }))
    })

    PushNotificationIOS.getInitialNotification().then(notification => {
        if (!notification) {
            return
        }
        const data = notification.getData()
        Alert.alert(JSON.stringify({ data, source: 'ClosedApp' }))
    })
    PushNotificationIOS.requestPermissions()

}

arnoldas500 avatar Apr 21 '20 18:04 arnoldas500

Hi, I think this issue is not only related to register. Same issue seems to occur on notification event and is not triggered.

Dallas62 avatar Apr 22 '20 09:04 Dallas62

Experiencing the same issue. Using react-native-push-notification which in turn uses this package. Any updates?

Edit: So the callback is only not triggered, when building the app in debug mode. Release build works.

schumannd avatar Apr 28 '20 13:04 schumannd

Same issue +1

hpiso avatar May 01 '20 06:05 hpiso

I managed to make it work by requesting the permission first, then calling the listener in the callback of checkPermission()

Example:

PushNotificationIOS.requestPermissions();

PushNotificationIOS.checkPermissions(function () {
    PushNotificationIOS.addEventListener('register', (token) => {
        console.log("MyAPNSTOKEN", token)
    })
});

I always get the apnsToken now, hope it can help

hpiso avatar May 01 '20 08:05 hpiso

Rebooting my device helped. Following this note: https://github.com/react-native-community/push-notification-ios/pull/97/files

lumberman avatar May 02 '20 03:05 lumberman

Same thing. Old project (worked fine few months ago) without any commits not working anymore — no errors, no token. Reboot not helped, trying another device — not working. I try to create new empty projects (0.62.0 and 0.62.2) only with this package — same result. My current project (0.62.2) working fine yesterday morning stoped working at evening )) How I can find APN log? Console, Flipper, Analytics Ddta show nothing.

Putting listeners in checkPermissions not helped too.

bragovo avatar May 06 '20 10:05 bragovo

certificates are up-to-date?

lumberman avatar May 08 '20 02:05 lumberman

Sure. image

Capabilities enabled: image

I can upload empty example app on github.

bragovo avatar May 08 '20 06:05 bragovo

https://github.com/bragovo/push-example - this is example app. Alert in simulator and nothing on any real devices (7, XS, SE).

bragovo avatar May 08 '20 07:05 bragovo

I'm using [email protected] and [email protected]. And the following codes are working for me.

// e.g. 1
PushNotificationIOS.requestPermissions();
PushNotificationIOS.addEventListener('register', (token) => {
    console.log("MyAPNSTOKEN", token);
});

// e.g. 2
PushNotificationIOS.requestPermissions().then(() => {
    PushNotificationIOS.addEventListener('register', (token) => {
        console.log("MyAPNSTOKEN", token);
    });
});

Thanks for @hpiso's comment.

xiaobo1234 avatar May 09 '20 07:05 xiaobo1234

Any news on this issue? Facing the same problem.

EDIT: Ok after many investigations, I concluded it's a phone/apple cache problem. I had to uninstall the app, uninstall test flight, stop wifi & reboot the phone 2 times. (messing between testflight version & dev version probably didn't help).

And tada! It magically worked again! So as said @lumberman, rebooting phone/uninstalling app and trying to clean the phone should help. Hope it'll help, good luck!

titoasty avatar Jun 12 '20 12:06 titoasty

I was able to get the token by making changes to my AppDelegate.mm file. Removed all notification related code recommended by README install section and replaced with: //top of file: #import <RNCPushNotificationIOS.h> #import <UserNotifications/UserNotifications.h>

//bottom of file before @end #pragma mark - Notifications

// Required to register for notifications

  • (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings]; } // Required for the register event.
  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } // Required for the notification event. You must call the completion handler after handling the remote notification.
  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; } // Required for the registrationError event.
  • (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error]; } // Required for the localNotification event.
  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [RNCPushNotificationIOS didReceiveLocalNotification:notification]; }

Hope this helps others who are having the same issue of not producing an APNS token.

arnoldas500 avatar Jun 12 '20 19:06 arnoldas500

I was able to get the token by making changes to my AppDelegate.mm file. Removed all notification related code recommended by README install section and replaced with: //top of file: #import <RNCPushNotificationIOS.h> #import <UserNotifications/UserNotifications.h>

//bottom of file before @EnD #pragma mark - Notifications

// Required to register for notifications

  • (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings]; } // Required for the register event.
  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } // Required for the notification event. You must call the completion handler after handling the remote notification.
  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; } // Required for the registrationError event.
  • (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error]; } // Required for the localNotification event.
  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [RNCPushNotificationIOS didReceiveLocalNotification:notification]; }

Hope this helps others who are having the same issue of not producing an APNS token.

this is EXACTLY the same code as the documentation

orberkov avatar Sep 20 '20 05:09 orberkov

I had the same problem, I checked AppDelegate and checked a few "fixes" and I found a solution. I'm firstly set up a listener and after that, I requestPerrmissions (previously I've used listeners when an alert from requestPermissions was true). I found this idea here. So currently (pseudocode):

const registerPushNotifications = async () => {
  PushNotificationIOS.addEventListener('register', deviceToken => {
    I'm saving Token from here
  })
  PushNotificationIOS.addEventListener('notification', showNotification)
}

const unregisterPushNotifications = () => {
  PushNotificationIOS.removeEventListener('notification', showNotification)
  PushNotificationIOS.removeEventListener('register', registerTokenCallback)
}

const someFunction = async () => {
  await registerPushNotifications();
  
  PushNotificationIOS.requestPermissions().then(def => {
     if (!def.alert) {
      unregisterPushNotifications()
     }
   })
}

Darex1991 avatar Mar 24 '22 14:03 Darex1991