0

我正在使用 zo0r react-native-push-notification库。

出于某种原因,当我在移动设备(或)app上安装它后第一次打开它时,令牌返回为空。iOSAndroid

    PushNotification.configure({

onRegister: function(token) {
    console.log( 'TOKEN:', token );
},

onNotification: function(notification) {
    console.log( 'NOTIFICATION:', notification );
},

senderID: "YOUR GCM SENDER ID",

permissions: {
    alert: true,
    badge: true,
    sound: true
},

popInitialNotification: true,
requestPermissions: true, 
});

我的问题是,如果 onRegister 方法将令牌返回为 null,那么获取令牌的最佳方法是什么?我必须在应用程序的其他屏幕中调用此功能吗?

 "react": "16.0.0-alpha.6",
 "react-native": "^0.44.3",
 "react-native-push-notification": "^3.0.1"

谢谢您的帮助,

米奇

4

2 回答 2

2

我有同样的问题。我不知道为什么,但PushNotification.configure在 iOS 上不起作用,所以你应该使用

const fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
} else {
// user doesn't have a device token yet
}

而不是 PushNotification.configure,(如果您使用await,请不要忘记使用async),或其他方式:

firebase.messaging().getToken()
.then(fcmToken => {
 if (fcmToken) {
  // user has a device token
 } else {
  // user doesn't have a device token yet
 } 
});

而对于Android,我上面发送的方法不起作用。但是 PushNotification.configure 有效。您应该使用 PushNotification.configure 来获取 android 的令牌。您应该检查您的 SDK、firebase-messaging 版本并更新您的 android studio,因为旧版本的 Android studio 不支持新版本的 firebase-messaging。在我做完这些事情后,我在 Android 上成功获得了我的令牌。如果您在更新这些内容时遇到问题,我可以向您发送所有版本的详细信息以及如何更新它们。

于 2021-05-26T05:43:59.743 回答
0

这个问题是FCM.getAPNSToken()在之后运行的FCM.requestPermissions(),所以当方法运行时应用程序没有获得推送通知的权限。

改变:

componentDidMount -> async componentDidMount 

FCM.requestPermissions(); -> await FCM.requestPermissions();

这个解决方案是https://github.com/evollu/react-native-fcm/issues/528

于 2018-05-28T12:32:39.560 回答