Skip to content

OnMessage not fired when app is in background - IOS #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tezine opened this issue Feb 8, 2019 · 3 comments
Closed

OnMessage not fired when app is in background - IOS #37

tezine opened this issue Feb 8, 2019 · 3 comments

Comments

@tezine
Copy link

tezine commented Feb 8, 2019

Hi,
Firebase C++ is working great on Android, but I'm facing some issues with IOS.
I'm sending a data message to one iOS Device like this:

{
	"to":"mykey",
	"data":{
		"title": "hello",
    	        "body": "this is body",		
		"key1":"value1",
		"key2":"value2",
		"message":"message X"
	},
	"priority":"high"
}

The message above fires the C++ OnMessage when the iOS app is in foreground, but it's not received when it's in background.
Any ideas on how to make it work and also display a notification when a message is received in background?

Here's my AppDelegate.m:

@interface QIOSApplicationDelegate
@end
//! Add a category to QIOSApplicationDelegate
@interface QIOSApplicationDelegate (QFacebookApplicationDelegate) //<UNUserNotificationCenterDelegate>
@end
NSString *const kGCMMessageIDKey = @"gcm.message_id";
@implementation QIOSApplicationDelegate (QFacebookApplicationDelegate)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	// Override point for customization after application launch.
	NSLog( @"Firebase configured.==============================================" );
    [FIRApp configure];
    
    // [START set_messaging_delegate]
    [FIRMessaging messaging].delegate = self;
    // [END set_messaging_delegate]
    
    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if ([UNUserNotificationCenter class] != nil) {
        // iOS 10 or later
        // For iOS 10 display notification (sent via APNS)
        [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
        UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter]
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
             // ...
         }];
    } else {
        // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    
    [application registerForRemoteNotifications];
    // [END register_for_notifications]
	return YES;
}
@end

I'm using Firebase CPP 5.4.4
Thank you,
Bruno.

@jonsimantov
Copy link
Contributor

You shouldn't need any of that notifications stuff in your app delegate if you are using the C++ SDK. The SDK uses swizzling to hook into those notifiers directly.

Could you see if you are able to reproduce the issue using the C++ quick start? (this GitHub project). And also if you remove all of the notifications-related code from your app delegate.

@chkuang-g
Copy link
Collaborator

@tezine Just to follow-up. Are you able to resolve your issue?

@stewartmiles
Copy link
Contributor

@tezine the message is not received in the background on iOS until the user taps on the message in the notification tray (see https://firebase.google.com/docs/cloud-messaging/ios/receive#handle_messages_received_through_the_apns_interface ). iOS doesn't really let you run your whole application in the background for a long time like Android. We're likely to add support for iOS extensions for Cloud Messaging to allow you to take action when a message is received like customization of the notification or caching, but that's all we can do I'm afraid.

As @jonsimantov and @chkuang-g mentioned, you don't need any custom AppDelegate code in the C++ SDK, we hook everything for you. If you follow the sample code (in this repo) you'll see what you need.

@firebase firebase locked and limited conversation to collaborators Oct 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants