简体   繁体   English

当在后台收到通知时,Objective-c徽章会依靠应用程序图标吗?

[英]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? 就是说,在最小化应用程序的同时收到通知时,我的代码/日志从不会触发(请参阅日志:“ NSLog应用程序处于后台”),我不确定为什么吗?

- (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 . 当您的应用程序在后台运行时,您的应用程序将不会收到didReceiveRemoteNotification调用, 除非用户点击通知警报

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. 1)当您的应用程序在后台(或挂起)并且收到远程通知时,将显示iOS系统警报。

2) If the user opens your app by tapping the notification alert, then your app will move the foreground and didReceiveRemoteNotification will be called. 2)如果用户通过点击通知警报打开您的应用程序,则您的应用程序将移动前台并调用didReceiveRemoteNotification

3) If the user ignores the notification, or dismisses it, then your app remains in the background, and didReceiveRemoteNotification will not be called . 3)如果用户忽略或关闭了该通知,则您的应用程序将保留在后台,并且didReceiveRemoteNotification 将不会被调用

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. 您的推送通知有效负载可以包含一个密钥,当系统收到您的通知时,iOS会使用该密钥设置应用程序徽章。

You simple include the key badge in your notification's payload: badge在通知的有效负载中包含关键badge

{
    "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: 我建议您看一下Apple的有关创建远程通知有效内容的文档,其中介绍了所有选项:

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification?language=objc 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. 这一切都这样说,这可以确保的iOS调用didReceiveRemoteNotification当你的应用程序是在后台。

To achieve this, you need to set the content-available parameter in your payload and send a "silent" notification. 为此,您需要在有效负载中设置content-available参数并发送“静默”通知。 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. 静默通知意味着用户将永远不会看到警报,但是您的应用将在有限的时间内静默地出现在前台,并会调用didReceiveRemoteNotification

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 https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_updates_to_your_app_silently?language=objc

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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