反应本机 ios 将呈现通知函数不处理前台的推送通知



我想用react native firebase消息处理react nativeFCM。对于IOS,我在AppDelegate.m中添加了必需的代码我添加了willPresentNotification功能来处理应用程序在前台时的推送:

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

目前,我没有在前台收到推送,我想原生地处理这个问题。有什么建议吗?

默认情况下,当应用程序处于foreground时,不会显示远程推送通知

您必须添加UNUserNotificationCenterDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
// ...
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

AppDelegate.h中添加UNUserNotificationCenterDelegate

#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
@property (nonatomic, strong) UIWindow *window;
@end

另一种方法是使用相同的远程通知,并将其呈现为类似的localNotification

最新更新