应用程序在后台时如何增加徽章编号 [英] How to increase badge number when application is in background

查看:105
本文介绍了应用程序在后台时如何增加徽章编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码。当推送通知到来时,每个东西都正常工作但是当应用程序在后台时,徽章编号不会增加。如何解决这个问题呢?

I am using this code. Every thing is working fine when push notification comes but badge number does not increase when application is in background. How to solve this problem?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    //UIApplication *application = [UIApplication sharedApplication];
    NSInteger badgeNumber = [application applicationIconBadgeNumber];// Take the current badge number
    //badgeNumber--;    // decrement by one
    [application setApplicationIconBadgeNumber:[[[launchOptions valueForKey:@"aps"]valueForKey:@"badge"]integerValue]];  // set ne badge number

    NSLog(@"userInfo :%@  %d",launchOptions,[[[launchOptions valueForKey:@"aps"]valueForKey:@"badge"]integerValue]);


    return YES;
}


推荐答案

当应用程序在background didFinishLaunchingWithOptions 方法永远不会调用。为了在你的应用程序处于后台时做某事,你需要在 AppDelegate的 applicationDidEnterBackground :方法中实现你的逻辑。

When the application is in the background didFinishLaunchingWithOptions method never calls. For doing something when your App is in background you need to implement your logic in AppDelegate's applicationDidEnterBackground: method like.

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

[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}

这篇关于应用程序在后台时如何增加徽章编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆