使用远程通知更新徽章计数

时间:2014-06-17 08:04:53

标签: objective-c ios7 apple-push-notifications

我的应用基于创建新活动。每当任何成员创建的新事件时,所有设备都会获得新事件的推送通知。为此,我在“事件”按钮上设置了徽章编号。

当应用程序正在运行时它正常工作但是当应用程序处于非活动状态或处于后台模式时,委托方法不起作用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    if(application.applicationState == UIApplicationStateInactive)
    {
        NSLog(@"inactive");
    }
    else if (application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"active");
    }
    else
    {
        NSLog(@"background");
    }
    NSString *badgenum=[[NSUserDefaults standardUserDefaults]objectForKey:@"eventbadges"];
    NSString *badgestr = [NSString stringWithFormat:@"%d",[badgenum intValue]+1];
    [[NSUserDefaults standardUserDefaults]setObject:badgestr forKey:@"eventbadges"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updatebadges" object:self];
}

0 个答案:

没有答案