推送通知的徽章计数没有增加.徽章计数始终保持为 1? [英] Badge Count is not increasing for push notification.always badge count remains 1?
问题描述
当应用程序在后台推送通知时,我的应用程序徽章计数不增加.计数仅在第一个推送通知时增加 1,并且始终保持徽章计数为 1,如果我收到超过 1 个通知,徽章计数也只剩下 1.下面是我的代码
My app badge count in not increasing when app is in background for push notifications.Count increase by 1 only for the first push notification and always remains badge count as 1, if i get more then 1 notification also badge count remaing 1 only. Below is my code
- (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 完成的.这是我的数据库
You have to do it from the server side. In my case I have done it through php and mysql. Here is my database
我添加了一个字段badgecount,每次使用此代码向设备发送推送时,我都会增加徽章计数
I have added a field badgecount and i increase the badge count every time i send the push to the device with this code
$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 来制作在
And I also make another api for making the badgcount 0 which is called in the
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
所以当看到通知时,它在服务器中再次为零.
So when the notification is seen it is again zero in the server.
这篇关于推送通知的徽章计数没有增加.徽章计数始终保持为 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!