React Native Ios推送通知不起作用

时间:2019-12-29 09:31:13

标签: ios react-native push-notification

我使用'@react-native-firebase/messaging'模块发送通知。在Android上,一切正常,下面是在iOS设备上尝试const fcmToken = await firebase.messaging().getToken();时收到的错误日志。

  

NativeFirebaseError:[消息/未知]操作无法完成。 (com.firebase.iid错误1001。)

我已经实施了“ react-native-permissions”来授予通知权限。

我的AppDelegate.m包含:

if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
 }

我还应该添加其他内容吗? 任何帮助或建议将非常有帮助。 预先感谢

2 个答案:

答案 0 :(得分:1)

有很多解决此问题的方法。由于某些StackOverflow用户的要求,我发布了我的答案。 这是我的代码

  const sleep = ms => {
    return new Promise(resolve => setTimeout(resolve, ms));
  };

  async function requestPermissionForNotification() {
    try {
      const permission = await requestNotifications(["alert", "badge", "sound"]);
      if (permission.status === "granted") {
        setupNotification();
      }
    } catch (e) {
      console.log(e)
    }
  };
  async function setupNotification() {
    try {

      await sleep(5000)
      // TODO no need token for now, Will be used for future releases 
      const enabled = await firebase.messaging().hasPermission();
      await requestNotifications(['alert', 'badge', 'sound']);
      await firebase.messaging().registerForRemoteNotifications();
      const token = await firebase.messaging().getToken();
     
      firebase.messaging().onMessage(async (remoteMessage) => {
   
      });
    } catch (e) {
      console.log(e)

    }
  }

而且我已经在Info.plist中添加了以下内容

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

总而言之,我使用react权限来授予权限,sleep方法是等待firebase准备就绪。终于卸载了应用程序几次,以获得结果。 当它开始工作时,因为花了我几天的时间,所以我不敢接触代码。

答案 1 :(得分:0)

在iOS中获取fcm令牌之前,您需要检查并询问消息传递权限

/** * Check is notification showing permission enabled if not ask the permission. */ async checkFcmPermission() { firebase .messaging() .hasPermission() .then(enabled => { if (enabled) { // User has permissions this.getFcmToken(); // const fcmToken = await firebase.messaging().getToken(); } else { // User doesn't have permission firebase .messaging() .requestPermission() .then(() => { // User has authorized this.getFcmToken(); // const fcmToken = await firebase.messaging().getToken(); }) .catch(error => { // User has rejected permissions console.log( 'PERMISSION REQUEST :: notification permission rejected', ); }); } }); }