加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 徽章计数没有增加推送通知.总计徽章计数仍为1?

发布时间:2020-12-15 01:41:43 所属栏目:百科 来源:网络整理
导读:当应用程序处于推送通知的后台时,我的应用程序徽章数量不会增加.对于第一个推送通知,数量仅增加1,并且始终将徽章计数保留为1,如果我得到超过1个通知,则徽章计数仅保留1个. 以下是我的代码 - (void)application:(UIApplication *)application didReceiveRemot
当应用程序处于推送通知的后台时,我的应用程序徽章数量不会增加.对于第一个推送通知,数量仅增加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,并且每次使用此代码将推送发送到设备时我都会增加徽章计数

$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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读