我将配置设置为通过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中显示?
答案 0 :(得分:0)
我通过添加react-native-permissions解决了这个问题。
import {requestNotifications} from 'react-native-permissions';
并在检查权限时执行此操作,
await requestNotifications(['alert', 'badge', 'sound']);
您将开始收到推送通知
希望对您有所帮助。感觉。免费获得疑问