【问题标题】:Objective-c badge count on app icon when notification received in background?在后台收到通知时,应用程序图标上的 Objective-c 徽章计数?
【发布时间】:2019-04-14 05:05:39
【问题描述】:

当应用在后台运行时收到通知时,我正在使用以下代码在我的应用图标上设置一个徽章。也就是说,当应用程序最小化时收到通知时,我的代码/日志永远不会触发(请参阅日志:“NSLog APP WAS IN Background”),我不知道为什么?

- (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!");
     }

【问题讨论】:

  • 不需要像这样设置徽章,在通知负载中我们有一个键名 badge 在那个后端发送通知徽章计数,它由操作系统自动处理。跨度>
  • 必须给徽章权限?

标签: ios objective-c


【解决方案1】:

这是远程通知的正确行为。

您的应用不会在您的应用处于后台时收到didReceiveRemoteNotification 调用,除非用户点击通知警报

这是它的工作原理。

1) 当您的应用处于后台(或暂停)并收到远程通知时,会显示 iOS 系统警报。

2) 如果用户通过点击通知警报打开您的应用,那么您的应用将移动前台并调用didReceiveRemoteNotification

3) 如果用户忽略通知,或将其关闭,那么您的应用程序将保留在后台,并且didReceiveRemoteNotification 不会被调用

也就是说,无需在代码中设置应用程序徽章。您的推送通知负载可以包含一个密钥,当系统收到您的通知时,iOS 使用该密钥设置应用程序徽章。

您只需在通知的有效负载中包含密钥 badge

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

我建议您查看 Apple 的用于创建远程通知负载的文档,其中解释了所有选项:

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

话虽如此,当您的应用在后台时, 可以确保 iOS 调用 didReceiveRemoteNotification

为此,您需要在有效负载中设置content-available 参数并发送“静默”通知。静默通知意味着用户永远不会看到警报,但您的应用会在有限的时间内静默地被带到前台,并且会调用didReceiveRemoteNotification

不过,这不是您的方案的合适选择。它旨在更新应用的内容,而不仅仅是更新徽章。

但是,如果您对静默通知感兴趣,可以在此处查看文档:

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

【讨论】:

    【解决方案2】:
    [UIApplication sharedApplication].applicationIconBadgeNumber = 3;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2021-11-21
      相关资源
      最近更新 更多