【发布时间】:2020-05-06 21:37:04
【问题描述】:
我设置了通过react-native-firebase获取远程推送通知的配置。在android中,一切都是正确的,但在ios中,我可以获得FCMToken,但我无法接收弹出推送通知。 podFile 是:
pod 'Firebase/Core'
pod 'Firebase/Auth'
pod 'Firebase/Messaging'
pod 'GoogleIDFASupport'
我的 AppDelegate.m 是:
#import <Firebase.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FIRApp configure];
[RNFirebaseNotifications configure];
...
}
- (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];
}
我获取 FCm 令牌并显示的代码是:
componentDidMount() {
const channel = new firebase.notifications.Android.Channel(
'insider',
'insider channel',
firebase.notifications.Android.Importance.Max,
);
firebase.notifications().android.createChannel(channel);
this.checkPermission();
this.createNotificationListeners();
}
async getToken() {
let tokenFCM = await AsyncStorage.getItem('tokenFCM');
console.log(tokenFCM);
if (!tokenFCM) {
// console.log('token not find');
try {
tokenFCM = await firebase.messaging().getToken();
if (tokenFCM) {
await AsyncStorage.setItem('tokenFCM', tokenFCM);
// do sth here
}
} catch {
console.log('token not find2');
}
} else {
// console.log('token is find');
}
}
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
async requestPermission() {
try {
await firebase.messaging().requestPermission();
this.getToken();
} catch (error) {
await AsyncStorage.setItem('tokenFCM', '');
console.log('permission rejected');
}
}
createNotificationListeners() {
firebase.notifications().onNotification(notification => {
notification.android.setChannelId('insider').setSound('default');
notification.android.setSmallIcon('@mipmap/...');
firebase.notifications().displayNotification(notification);
});
}
为什么我可以在 Android 中看到推送通知,但在 ios 中看不到?
【问题讨论】:
-
您遇到的错误是什么?
-
我没有错误,但我无法收到任何通知。我应该创建开发者帐户吗?
-
你试过我的解决方案了吗?是的,您需要一个开发者帐户
标签: xcode react-native push-notification react-native-firebase