ios - iOS Firebase 通知仅出现在后台
问题描述
我有一个添加了 Firebase 消息的 iOS 应用。问题是 Firebase 推送通知仅在应用程序终止或在后台出现时才会出现。我希望能够以编程方式处理消息并显示某种模式,但didReceiveRemoteNotification
甚至没有调用该方法。
这是我的AppDelegate.m
#import "AppDelegate.h"
#import "MainViewController.h"
#import "AppName-Swift.h"
@import Firebase;
@import FirebaseMessaging;
@import FirebaseInstanceID;
@implementation AppDelegate {
LocationSyncManager* locationSyncManager;
bool isLocationLaunch;
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
isLocationLaunch = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
[FIRApp configure];
[FIRMessaging messaging].delegate = self;
[self enablePushNotifications:application];
if(isLocationLaunch) {
if(AccountStore.shared.account != nil) {
locationSyncManager = [LocationSyncManager shared];
[locationSyncManager enable];
}
} else {
self.viewController = [[MainViewController alloc] init];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
return nil;
}
- (void) enablePushNotifications:(UIApplication*)application {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
- (void)messaging:(nonnull FIRMessaging *)messaging didReceiveRegistrationToken:(nonnull NSString *)fcmToken {
[LocationSyncManager shared].deviceToken = fcmToken;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[FIRMessaging messaging].APNSToken = deviceToken;
[FIRMessaging messaging].shouldEstablishDirectChannel = YES;
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"--- didFailToRegisterForRemoteNotificationsWithError %@", error);
}
// In case the user tries to open the app while it is running in the background,
// allow the webview to initialize and disable the isLocationLaunch flag.
- (void)applicationWillEnterForeground:(UIApplication *)application {
if(isLocationLaunch) {
self.viewController = [[MainViewController alloc] init];
[self application:application didFinishLaunchingWithOptions:nil];
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"---- RECEIVED REMOVE NOTIFICATION %@", userInfo);
}
@end
---- RECEIVED REMOVE NOTIFICATION
服务器发送 PN 后,我应该在日志中看到,但它没有发生。为什么不在didReceiveRemoteNotification
前台调用?
解决方案
推荐阅读
- python - Python提取数据并将其附加到数据框中
- database - python中的持久数据存储
- uwp - UWP EasClientDeviceInformation.SystemFirmwareVersion 始终返回 0
- wordpress - 如何从 wordpress 中使用 WpDataTable 插件构建的表中提取数据?
- ios - Swift imageView 动画不起作用
- c# - 更改默认操作路线
- sonarqube - 声纳给 NullPointerException 不正确
- r - 读取包含韩语的 CSV 时出错
- javascript - 循环遍历函数属性,可能吗?
- angular - 如何在 Angular 4 PUT 请求中分配标头并使用请求正文?