react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
745 stars 285 forks source link

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

Open arnoldas500 opened 4 years ago

arnoldas500 commented 4 years ago

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()

}

Dallas62 commented 4 years ago

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

schumannd commented 4 years ago

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.

hpiso commented 4 years ago

Same issue +1

hpiso commented 4 years ago

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

lumberman commented 4 years ago

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

bragovo commented 4 years ago

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.

lumberman commented 4 years ago

certificates are up-to-date?

bragovo commented 4 years ago

Sure. image

Capabilities enabled: image

I can upload empty example app on github.

bragovo commented 4 years ago

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

xiaobo1234 commented 4 years ago

I'm using react@16.8.6 and react-native@0.60.6. 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.

titoasty commented 4 years ago

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!

arnoldas500 commented 4 years ago

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

import <UserNotifications/UserNotifications.h>

//bottom of file before @end

pragma mark - Notifications

// Required to register for notifications

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

orberkov commented 4 years ago

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

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

Darex1991 commented 2 years ago

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()
     }
   })
}