zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.77k stars 2.05k forks source link

token not being passed / onRegister not working IOS #1846

Closed digitlninja closed 3 years ago

digitlninja commented 3 years ago

Bug

Environment info

react-native info output:

System:
    OS: macOS 11.1
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 1.46 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.7.0 - /usr/local/bin/node
    Yarn: Not Found
    npm: 6.14.7 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.0. - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.2, DriverKit 20.0, macOS 11.0, tvOS 14.2, watchOS 7.1
    Android SDK:
      API Levels: 28, 29, 30
      Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.2
      System Images: android-29 | Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom_64, android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.0 AI-193.6911.18.40.6626763
    Xcode: 12.2/12B45b - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_265 - /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.63.3 => 0.63.3
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: 7.2.0

Steps To Reproduce

  1. Start the app react-native run-ios The request permission popup displays, but the token is not logged (or alerted) I need to get the token to move forward with dev.

Describe what you expected to happen:

  1. The token to be logged / alerted after permission granted (in onRegister as in your docs)

Reproducible sample code

/initialize.js

export const configureApn = () => {
    PushNotification.configure({
        // user accepted notification permission - register token
        onRegister: function (tokenData) {
            const { token } = tokenData;
            window.alert(token);
            console.log('APN TOKEN PLEASE', token);
            // handle device token
            // send token to server...
            // store in AsyncStorage...
        },
        // notification received / opened in-app event
        onNotification: function (notification) {
            notification.finish(PushNotificationIOS.FetchResult.NoData);
        },
        // outlining what permissions to accept
        permissions: {
            alert: true,
            badge: true,
            sound: true
        },
        popInitialNotification: true,
        requestPermissions: true
    });
};

app/index.js
import { configureApn } from './initialize';

configureApn();
AppRegistry.registerComponent(appName, () => App);

Im lost or this is just not working. I've followed the docs: npm install -> pod install -> using autolinking.

Thank you

Dallas62 commented 3 years ago

Hi @digitlninja Are you testing on real device ?

digitlninja commented 3 years ago

yes

Dallas62 commented 3 years ago

Did you follow the ios installation instructions ? (On the iOS repository)

digitlninja commented 3 years ago

Yes, from: https://github.com/react-native-push-notification-ios/push-notification-ios#install

Dallas62 commented 3 years ago

Not able to reproduce with the exemple project. Please double check your installation and take a look to the exemple project. And make sure you tested with a real device, simulator doesn't support notifications.

digitlninja commented 3 years ago

What example project ?? Yes Im testing only on a real device. I need to sort this urgently

digitlninja commented 3 years ago

Ive double checked my install many times... the way its done in the example project is a different more complex architecture. Ive simply initialized it as above and it should give me the token / fire onRegister when the app starts.... it doesnt.

Dallas62 commented 3 years ago

https://github.com/zo0r/react-native-push-notification/tree/master/example

Dallas62 commented 3 years ago

Since I can't reproduce, I cannot help you more.

digitlninja commented 3 years ago

You havent helped at all though... what am I missing?

Dallas62 commented 3 years ago

I don't know I'm not able to reproduce.

digitlninja commented 3 years ago

I think I fixed it.. The docs say not to invoke configure from a lifecycle method. so how can one pass the token to components???

Dallas62 commented 3 years ago

You can store the token inside a variable, such as a global variable.