0

I have an iOS app with Firebase messaging added. The problem is the firebase push notifications only appear if the app is terminated or in the background. I'd like to be able to handle the message programmatically and display some kind of modal, but the didReceiveRemoteNotification method isn't even being called.

Here is my 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

I should be seeing ---- RECEIVED REMOVE NOTIFICATION in the logs after the server sends a PN, but it's not happening. Why isn't didReceiveRemoteNotification called in the foreground?

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
SimpleJ
  • 13,812
  • 13
  • 53
  • 93
  • 2
    since iOS 10, the method that handles this is `userNotificationCenter(_:willPresent:withCompletionHandler:)` . See this: https://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10 – janusfidel Aug 27 '18 at 19:00
  • @janusfidel Using `UNUserNotificationCenter` appears to solve the problem, but it's only supported in iOS 10 and up. Does that mean in-app notifications aren't possible in iOS 9 and below? – SimpleJ Aug 28 '18 at 16:06

0 Answers0