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

时间:2018-09-16 22:56:42

标签: ios objective-c

当应用在后台运行时收到通知时,我正在使用以下代码在应用图标上设置徽章。就是说,在最小化应用程序的同时收到通知时,我的代码/日志从不会触发(请参阅日志:“ 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!");
     }

2 个答案:

答案 0 :(得分:0)

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

您的应用在后台运行时,您的应用不会收到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

答案 1 :(得分:0)

train_images2 = []
for i in range(len(train_2)):
  im = process_image(Image.open(train_2['Path'][i]))
  train_images2.append(im)
train_images2 = np.asarray(train_images2)