2013-09-24 91 views
-1

當應用程序處於推送通知的後臺時,我的應用程序徽章數量不會增加。僅當第一次推送通知時,計數纔會增加1,並且始終將徽章計數保留爲1,如果我獲得的通知數量超過1個,則通知徽章計數僅爲1。 下面是我的代碼徽章數量沒有增加推送通知。總是徽章數量仍然是1?

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

    NSString *message = nil; 
    id alert = [userInfo objectForKey:@"aps"]; 
    if ([alert isKindOfClass:[NSString class]]) { 
     message = alert; 
    }  
    else if ([alert isKindOfClass:[NSDictionary class]]) { 
     message = [alert objectForKey:@"alert"]; 
    } 
    if (alert) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"xyz" 
                 message:message 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:@"Cancel", nil]; 
     alertView.tag=2525; 
     [alertView show]; 
    } 
} 


-(void)alertView:(UIAlertView *)alertView 
    clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if(alertView.tag==2525) { 
     [UIApplication sharedApplication].applicationIconBadgeNumber = 
     [UIApplication sharedApplication].applicationIconBadgeNumber-1; 
    } 
} 
+0

我看到很多關於推送通知和證書的類似問題,只是想知道這是從哪裏來的。 – zaph

+0

您的服務器發送給APNS的有效負載是什麼? – Moxy

+0

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];我使用的是以上有效載荷 – user2230971

回答

1

你說你的有效載荷是:

aps = { alert = "third testing"; badge = 1; sound = "sound.caf"; };

因爲你總是送1的徽章數量,這顯示了您的應用程序徽章計數。徽章數量不是增量式的。如果要查看徽章數量高於1,則服務器應將有效負載中的值設置爲高於1。

您的服務器應該跟蹤每個設備應該接收哪個徽章。該應用程序本身並不能保證接收所有推送通知,因此您不能依靠其邏輯來更新徽章計數。

+0

[UIApplication sharedApplication] .applicationIconBadgeNumber = [UIApplication sharedApplication] .applicationIconBadgeNumber + 1; 我能夠通過這條線增加徽章數量,但是。這裏有什麼問題是iam在選擇通知之後獲得徽章數量我需要在沒有選擇通知的情況下得到這個數字 – user2230971

+0

這是您不應該在應用程序中以編程方式增加徽章數量的另一個原因 - 您的代碼僅在用戶點擊通知後執行。在用戶查看通知後,應該只使用應用程序中的徽章來清除它(將其設置爲零)。 – Eran

+0

好吧,那麼我需要做些什麼來增加徽章數量呢? – user2230971

2

你必須從服務器端進行。在我的情況下,我已經通過PHP和MySQL做到了。這裏是我的數據庫 enter image description here

我添加一個字段badgecount,我增加了徽章計數每次我發推送到設備使用此代碼

 $query = "SELECT badgecount FROM pushnotifications WHERE device_token = '{$device_token}'"; 
     $query = $this->db->query($query); 
     $row = $query->row_array(); 
     $updatequery = "update pushnotifications set badgecount=badgecount+1 WHERE device_token ='{$device_token}'"; 
     $updatequery = $this->db->query($updatequery); 
     $device = $device_token; 
     $payload['aps'] = array('alert' => $pushmessage, 'badge' =>$row["badgecount"]+1, 'sound' => 'default'); 
     $payload = json_encode($payload); 
     ... 

而且我還讓另一個API製作的時間badgcount 0在

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

因此,當通知被看到它在服務器中再次爲零。