用于远程和本地通知的iOS徽章

时间:2014-07-03 06:49:31

标签: ios apple-push-notifications uilocalnotification badge

我的应用会收到两种类型的通知,推送和本地,我不知道管理徽章号码的最佳方式是什么。如果是本地通知,我会在创建一个徽章计数器时递增徽章计数器,但是在推送通知的情况下...如果我没有错,则仅在用户启动应用程序时调用委托方法didReceiveRemoteNotification:通过点击提醒或横幅,或者应用是否处于活动状态。否则,徽章的值取自通知的有效负载,不是吗?那么......考虑到收到的本地通知+远程通知,我怎么能总是有更新的徽章价值?

由于

1 个答案:

答案 0 :(得分:0)

您在远程通知中得到了响应,如下所示。

Connection OK sending message :{"aps":{"alert":"pushnotification testing","content-available":"","badge":"1","sound":"default"}} 134

在AppDelegte.h类

取一个整数变量int i=0;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationIconBadgeNumber = 0;

    UILocalNotification *localNoty=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNoty) {
        NSLog(@"Recieved Notification %@",localNoty);
    }

    return YES;
}

//收到通知

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    // Handle the notificaton when the app is running

    app.applicationIconBadgeNumber=i++;
    NSLog(@"Recieved Notification %@",notif);
}

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

#if !TARGET_IPHONE_SIMULATOR
    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);

    // set it to Zero will clear it.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

#endif
}

-(void) pw_setApplicationIconBadgeNumber:(NSInteger) badgeNumber
{
    [UIApplication sharedApplication].applicationIconBadgeNumber +=badgeNumber;
}

//在应用激活时设置徽章nil。

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    application.applicationIconBadgeNumber =nil;

}