React-native Firebase IOS推送通知

时间:2019-11-01 14:32:07

标签: firebase react-native react-native-ios react-native-firebase

我正在尝试使用FB进行推送通知。在android bud IOS上一切正常,但存在一些问题。 主要问题是我无法收到通知。 ->芽有时通知正确,当我重新安装应用程序通知时一切正常,不会在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,我已经准备好了所有设置,并且应该可以通过日志运行: enter image description here 一切都很好... 现在,我尝试将通知发送到手机,但没有任何反应。但是,如果我尝试将其直接发送到该令牌,它会起作用,我会收到通知。

enter image description here

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

有解决方案吗?

1 个答案:

答案 0 :(得分:0)

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

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