推送通知徽章计数不更新 [英] Push Notification Badge Count Not Updating

查看:183
本文介绍了推送通知徽章计数不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code苹果推送通知,应用程序运行时和通知来我正在递增徽章数量和获得期望的结果,当我点击home键,在应用程序图标。但是,当我没有运行我的应用程序和通知来了,它没有自动递增的徽章数量,仍然只有1.1从服务器来的价值。任何一个可以指出我在哪里做错了。先谢谢了。

   - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {
    userMessageCounter = @0;
    postType = 0;
    joinedStreamChecker = 0;
    OwnerValue = 0;
    pushValue = 1;
    badgeValue = 0;    //覆盖点后,应用程序启动定制。    //导航控制器的视图添加到窗口和显示。
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible]
    [UIApplication的sharedApplication]
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert |
      UIRemoteNotificationTypeBadge |
      UIRemoteNotificationTypeSound)];
    // [UIApplication的sharedApplication] registerForRemoteNotificationTypes:
    //(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];    [UIApplication的sharedApplication] .applicationIconBadgeNumber = 0;
    // [[UIApplication的sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];    UIRemoteNotificationType类型= [[UIApplication的sharedApplication] enabledRemoteNotificationTypes];
    如果(类型== UIRemoteNotificationTypeNone)
    {
        pushValue = 0;
        的NSLog(@关闭通知);
    }    返回YES;
}
- (无效)应用:(*的UIApplication)应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*)deviceToken1 {    * NSString的海峡= [的NSString
                     stringWithFormat:@%@,deviceToken1]
    的NSLog(@%@,STR);    self.deviceToken = [的NSString stringWithFormat:@%@,STR]
    的NSLog(@开发---%@,self.deviceToken);
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@< withString:@];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@withString:@];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@>中withString:@];
    的NSLog(@开发---%@,self.deviceToken);
} - (无效)应用:(*的UIApplication)应用didFailToRegisterForRemoteNotificationsWithError:(NSError *)错误{    * NSString的海峡= [的NSString stringWithFormat:@错误:%@,犯错]
    的NSLog(@%@,STR);} - (无效)应用:(*的UIApplication)的应用didReceiveRemoteNotification:(NSDictionary的*){用户信息
    的NSLog(@接到通知:%@,用户信息);
    // [自addMessageFromRemoteNotification:用户信息];    * NSString的alertValue = [[USERINFO valueForKey:@APS] valueForKey:@徽章];
    的NSLog(@我message--%@,alertValue);
    badgeValue = [alertValue的intValue];
    [UIApplication的sharedApplication] .applicationIconBadgeNumber + = badgeValue;
    // badgeValue = [UIApplication的sharedApplication] .applicationIconBadgeNumber;
    // [UIApplication的sharedApplication] .applicationIconBadgeNumber = 0;
    // [UIApplication的sharedApplication] setApplicationIconBadgeNumber:badgeValue];
}


解决方案

苹果没有保持跟踪你的数据,你。那只能说明你告诉它。因此,你必须存储在服务器上的计数,然后告诉苹果新的证件号码,当你发送警报。这通常是通过发动告诉你的服务器来零未读通知的计数当具有应用程序打电话回家完成。

this is my code for apple push notification, when the application is running and notification coming i am incrementing the badge count and getting desired result when i click home button, on app icon. but when i am not running my application and notification comes, it didn't auto increment badge count and remains to only 1. the value 1 is coming from server. can any one point out where i am doing wrong. thanks in advance.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    userMessageCounter = @"0";
    postType = 0;
    joinedStreamChecker = 0;
    OwnerValue = 0;
    pushValue = 1;
    badgeValue =0;

    // Override point for customization after application launch.

    // Add the navigation controller's view to the window and display.
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];


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


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

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    // [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];

    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone) 
    {
        pushValue = 0;


        NSLog(@"notification off");
    }

    return YES;
}


- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken1 { 

    NSString *str = [NSString 
                     stringWithFormat:@"%@",deviceToken1];
    NSLog(@"%@",str);

    self.deviceToken = [NSString stringWithFormat:@"%@",str];
    NSLog(@"dev --- %@",self.deviceToken);
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"dev --- %@",self.deviceToken);


}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 

    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(@"%@",str);    

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"Received notification: %@", userInfo);
    //[self addMessageFromRemoteNotification:userInfo];

    NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
    NSLog(@"my message-- %@",alertValue);
    badgeValue= [alertValue intValue];
    [UIApplication sharedApplication].applicationIconBadgeNumber += badgeValue;
    //badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;
    //[UIApplication sharedApplication].applicationIconBadgeNumber=0;
    //[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];


}

解决方案

Apple does not keep track of your data for you. It only shows what you tell it to. Thus, you have to store the count on your server, then tell apple the new badge number when you send the alert. Typically this is done by having the app phone home when launched telling your servers to zero out the count of unread notifications.

这篇关于推送通知徽章计数不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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