简体   繁体   中英

Objective-c badge count on app icon when notification received in background?

I'm using the below code to set a badge on my app icon when a notification is received while the app is running in the background. That said, my code/log is never triggered when a notification is received while the app is minimized (see log: "NSLog APP WAS IN BACKGROUND") and I'm not sure why?

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

    NSLog(@"application Active - notication has arrived while app was opened");

    completionHandler(UIBackgroundFetchResultNewData);

    NSLog(@"Notification received when open");

    if(application.applicationState == UIApplicationStateInactive) {


        NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");

        completionHandler(UIBackgroundFetchResultNewData);

        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];


       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

       UITabBarController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
      [viewController setSelectedIndex:0];

        ;

        self.window.rootViewController = viewController;
       [self.window makeKeyAndVisible];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotificationReceived" object:nil];


    }

    if (application.applicationState == UIApplicationStateBackground) {

        NSLog(@"APP WAS IN BACKGROUND");

        static int i=1;
        [UIApplication sharedApplication].applicationIconBadgeNumber = i++;


    }

}

- (void)applicationDidEnterBackground:(UIApplication *)application{

    static int i=0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = i;
    NSLog(@"Triggered!");
     }

This is the correct behaviour for a remote notification.

Your app won't receive the didReceiveRemoteNotification call when your app is in the background, unless the user taps the notification alert .

Here's how it works.

1) When your app is in the background (or suspended) and a remote notification is received, an iOS system alert is shown.

2) If the user opens your app by tapping the notification alert, then your app will move the foreground and didReceiveRemoteNotification will be called.

3) If the user ignores the notification, or dismisses it, then your app remains in the background, and didReceiveRemoteNotification will not be called .

That said, there is no need to set the application badge in code. Your push notification payload, can include a key which iOS uses to set the application badge when the system receives your notification.

You simple include the key badge in your notification's payload:

{
    "aps" : {
        "alert" : {
            "title" : "Notification title",
            "body" : "Notification body"
        },
        "badge" : 5
    }
}

I'd suggest you take a look at Apple's documentation for creating a remote notification payload, which explains all the options:

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification?language=objc

That all being said, it is possible to make sure iOS calls didReceiveRemoteNotification when your app is in the background.

To achieve this, you need to set the content-available parameter in your payload and send a "silent" notification. A silent notification means that the user will never see an alert, but your app will be silently brought to the foreground for a finite amount of time, and didReceiveRemoteNotification will be called.

It's not an appropriate choice for your scenario, though. It's intended for updating an app's content, not just updating the badge.

However, if you're interested in silent notifications, you can see the documentation here:

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_updates_to_your_app_silently?language=objc

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM