应用程序在后台时如何增加徽章数量

Dev*_*ngh 5 iphone objective-c ios

我正在使用此代码。当推送通知出现时,一切正常,但当应用程序在后台时,徽章数量不会增加。如何解决这个问题呢?

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Irf*_*fan 2

当应用程序处于后台时,该didFinishLaunchingWithOptions方法永远不会调用。要在应用程序处于后台时执行某些操作,您需要在AppDelegate's applicationDidEnterBackground: 方法中实现您的逻辑。

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

[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}
Run Code Online (Sandbox Code Playgroud)

  • -(void)newNotification:(NSNotification *) notification{ NSDictionary *userInfo = notification.userInfo; if ([[NSString stringWithFormat:@"%@",userInfo] isEqualToString:@"0"]) { _notifiImageView.hidden = YES; _notificatinLbl.hidden = 是;} 其他{_notificatinLbl.hidden = 否; _notificatinLbl.hidden = 否;_notificatinLbl.text = [NSString stringWithFormat:@"%@",userInfo]; NSString * 徽章 = [NSString stringWithFormat:@"%@",userInfo]; [UIApplication共享应用程序].applicationIconBadgeNumber = badge.intValue; }} (3认同)