![](/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.