xcode 推送通知的徽章计数没有增加。徽章计数始终保持为 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18981945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:54:05  来源:igfitidea点击:

Badge Count is not increasing for push notification.always badge count remains 1?

iosxcodepush-notificationapple-push-notificationsbadge

提问by user2230971

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

当应用程序在后台推送通知时,我的应用程序徽章计数不增加。计数仅在第一个推送通知时增加 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;
   }
}

回答by souvickcse

You have to do it from the server side. In my case I have done it through php and mysql. Here is my database enter image description here

您必须从服务器端执行此操作。就我而言,我是通过 php 和 mysql 完成的。这是我的数据库 在此处输入图片说明

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); 
        ...

And I also make another api for making the badgcount 0 which is called in the

我还制作了另一个用于制作 badgcount 0 的 api,它在

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

So when the notification is seen it is again zero in the server.

因此,当看到通知时,它在服务器中再次为零。

回答by Eran

You said your payload is :

你说你的有效载荷是:

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

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

Since you always send 1for badge count, that's the badge count being displayed for your app. The badge count is not incremental. If you want to see badge counts higher than 1, your server should put values higher than 1 in your payload.

由于您总是发送1徽章计数,这就是为您的应用显示的徽章计数。徽章计数不是递增的。如果您想看到徽章计数高于 1,您的服务器应该在您的有效负载中放置高于 1 的值。

Your server should keep track of which badge count each device should receive. The app itself is not guaranteed to receive all the push notifications, so you can't rely on its logic to update the badge count.

您的服务器应跟踪每个设备应收到的徽章计数。应用程序本身不能保证收到所有推送通知,因此您不能依赖其逻辑来更新徽章计数。