我正在将Firebase云消息传递集成到React Native应用程序中,并且在iOS上有一个很好的问题:如果应用程序在后台,通知会按预期显示,但如果应用程序在前台,则没有任何反应。< / p>
我已经从RNFirebase文档中添加了AppDelegate.m代码,包括我理解为iOS 10新增的withCompletionHandler变体:
// From https://rnfirebase.io/docs/v3.3.x/messaging/ios:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[RNFirebaseMessaging didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
[RNFirebaseMessaging didReceiveRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
[RNFirebaseMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[RNFirebaseMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
[RNFirebaseMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
在react-native应用程序中,我有一个onMessage处理程序,它可以在iOS模拟器和Android上宣传:
// ...Initializing Firebase...
messaging.onMessage(handleMessage);
// Handle a message:
function handleMessage(message) {
console.log('Firebase: Received message:', message);
const theActualMessage = message.fcm ? message.fcm : message.notification;
if (typeof message.finish === 'function') {
message.finish();
}
Alert.alert('Push message', theActualMessage.body);
}
无论是从Firebase控制台还是通过HTTP API发送推送通知,如果应用程序位于后台,通知仅会显示在iOS(在真实设备上)上;我永远不会调用handleMessage()
函数。
在iOS模拟器,Android模拟器和Android手机上,我的处理程序按预期调用 - 只有真正的iOS设备行为不端。