React-native firebase IOS 推送通知

React-native firebase IOS push notification

我正在尝试使用 FB 推送通知。在 android 上一切正常 bud IOS 有一些问题。 主要问题是我无法收到通知。 -> Bud 有时会正确收到通知,并且在我重新安装应用程序时一切正常,但 Bud 通知不会到达 iPhone。它随机工作。

我用

"react-native": "0.59.4"
"react-native-firebase": "^5.5.6"

我的 pod 文件

  pod 'Firebase/Core', '~> 6.3.0'
  pod 'Firebase/Messaging', '~> 6.3.0'

AppDelegate.m

#import <Firebase.h>
#import <UserNotifications/UserNotifications.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"
...

if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
}
[RNFirebaseNotifications configure];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings
 settingsForTypes: (UIUserNotificationTypeBadge |
                    UIUserNotificationTypeSound |
                    UIUserNotificationTypeAlert)
 categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

// Register the supported interaction types.

UIUserNotificationType types = UIUserNotificationTypeBadge |

UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =

[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

...

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
    [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

下面是我在代码中使用它的方式:

import firebase from 'react-native-firebase';

const getToken = () => {
  firebase.messaging().getToken().then((token) => {
    console.log('XXXXX getToken: ', token);
    firebase.messaging().ios.registerForRemoteNotifications();
  });
};

const requestPermission = () => {
  try {
    firebase.messaging().requestPermission().then((xx) => {
      console.log('XXXX requestPermission', xx);
    });
  } catch (error) {
    console.log('XXXX requestPermission User has rejected permissions');
  }
};

export const checkPermission = () => {
  // firebase.messaging().ios.registerForRemoteNotifications();

  firebase.messaging().hasPermission().then((permission) => {
    console.log('XXXXX hasPermission check: ', permission);
    if (permission) {
      console.log('XXXX hasPermission TRUE');
      getToken();
    } else {
      console.log('XXXX hasPermission FALSE');

      requestPermission();
    }
  }).catch((e) => {
    console.log('XXXXX hasPermission ERROR: ', e);
  });
};


export const listeners = () => {
  console.log("XXXXX Listeners setup OK!!");

  firebase.notifications().onNotificationDisplayed((notification) => {
    console.log("XXXXX onNotificationDisplayed", notification);
  });

  firebase.notifications().onNotification((notification) => {
    console.log("XXXXX LISTENER onNotification", notification);

  });
  firebase.notifications().onNotificationOpened((notificationOpen) => {
    console.log("XXXXX LISTENER onNotificationOpened");
  });
  firebase.notifications().getInitialNotification().then((notificationOpen)=> {
    console.log("XXXXX LISTENER getInitialNotification OK", notificationOpen);
  }).catch((error)=> {
    console.log("XXXXX LISTENER getInitialNotification ERROR");
  });
  firebase.messaging().onMessage((message) => {
    console.log("XXXXX LISTENER onMessage");
  });
}

我正在将此文件导入主要组件

 componentDidMount() {
    checkPermission();
    listeners();
  }

在 android 上一切正常,我能够接收通知并提取所有数据。 但是对于 ios,我已经准备好了所有设置,它应该可以通过日志运行: 一切看起来都很好... 现在我尝试向我的 phone 发送通知,但没有任何反应。但是,如果我尝试将它直接发送到该令牌,它会起作用,我会收到通知。

有时它会开始工作,当我重新安装应用程序时我会收到通知,但它不再工作了。

有什么解决办法吗?

我的解决方案: 更新椰子。所有问题都消失了

  pod 'Firebase/Core', '~> 6.5.0'
  pod 'Firebase/Messaging', '~> 6.5.0'