首页 > 解决方案 > 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前台调用?

标签: iosobjective-cfirebasefirebase-cloud-messaging

解决方案


推荐阅读