【问题标题】:React Native Ios Push Notification Not WorkingReact Native Ios 推送通知不起作用
【发布时间】:2023-03-17 06:22:01
【问题描述】:

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

NativeFirebaseError: [messaging/unknown] 操作无法完成。 (com.firebase.iid 错误 1001。)

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

我的 AppDelegate.m 包含:

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

我应该添加其他东西吗? 任何帮助或建议都会非常有帮助。 提前致谢

【问题讨论】:

  • 如果没有解决,请告诉我。
  • @AnkitPatidar 我已经发布了我的解决方案,希望对您有所帮助
  • 查看解决方案here by jacobp100 the Workaround.m

标签: ios react-native push-notification


【解决方案1】:

在 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',
              );
            });
        }
      });
  }

【讨论】:

    【解决方案2】:

    这个问题有很多解决方案。由于一些 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准备好。最后卸载了几次应用程序以获得结果。 当它开始工作时,我花了好几天的时间都不敢碰代码。

    【讨论】:

    • 权限在 5 秒后而不是之前询问,用户不会收到任何通知
    猜你喜欢
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多