![](/img/trans.png)
[英]Remote Push Notification badge count not increasing when app is in background
[英]Badge Count is not increasing for push notification.always badge count remains 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;
}
}
您必須從服務器端執行此操作。 就我而言,我是通過php和mysql完成的。 這是我的數據庫
我添加了一個字段badgecount,並且每次使用此代碼將推送發送到設備時,我都會增加badge的數量
$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);
...
我還制作了另一個使badgcount 0的api,該API在
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
因此,當看到通知時,服務器中的通知再次為零。
您說您的有效載荷是:
aps = { alert = "third testing"; badge = 1; sound = "sound.caf"; };
由於您始終發送1
來獲得徽章計數,因此這就是為您的應用顯示的徽章計數。 徽章計數不是增量的。 如果要查看徽章計數大於1,則服務器應在有效負載中放入大於1的值。
您的服務器應跟蹤每個設備應接收的徽章計數。 該應用程序本身不能保證會收到所有推送通知,因此您不能依靠其邏輯來更新徽章計數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.