iOS Firebase通知仅在后台显示

时间:2018-08-27 18:37:33

标签: ios objective-c firebase firebase-cloud-messaging

我有一个添加了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

在服务器发送PN之后,我应该在日志中看到---- RECEIVED REMOVE NOTIFICATION,但没有发生。为什么不didReceiveRemoteNotification在前台调用?

0 个答案:

没有答案