2012-03-16 109 views
2

這是我的蘋果推送通知的代碼,當應用程序正在運行並且通知即將到來時,我正在遞增徽章計數並獲得所需結果,當我單擊主頁按鈕時,應用程序圖標上。但是當我沒有運行我的應用程序併發出通知時,它不會自動增加徽章計數並保持爲1.值1來自服務器。任何人都可以指出我做錯了什麼。提前致謝。推送通知徽章計數不更新

- (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]; 


} 
+0

[Push Notification Badges not Coming]的可能重複(http://stackoverflow.com/questions/9734451/push-notification-badges-notcoming) – jrturton 2012-03-16 18:31:20

+0

我沒有得到答案,這就是爲什麼我在這裏發佈的問題。 – 2012-03-16 18:33:31

+1

這不是它的工作原理。編輯您的原始問題或添加賞金。無論如何,你說它的原始工作正常? – jrturton 2012-03-16 18:37:39

回答

4

Apple不會爲您保留您的數據。它只顯示你告訴它的內容。因此,您必須將計數存儲在您的服務器上,然後在發送提醒時告訴蘋果新的徽章號碼。通常,這是通過在啓動時將應用程序電話置於家中,告訴您的服務器清除未讀通知的數量來完成的。

+0

我的服務器如何知道該人已閱讀或不讀取該消息? – 2012-03-16 18:32:21

+2

這是給你弄清楚的。簡單的方法是打開應用程序時都使用0。 – coneybeare 2012-03-16 18:33:21

+0

iam通過將代碼放入應用程序完成啓動[UIApplication sharedApplication] .applicationIconBadgeNumber = 0; – 2012-03-16 18:34:58