iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 徽章计数没有增加推送通知.总计徽章计数仍为1?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

当应用程序处于推送通知的后台时,我的应用程序徽章数量不会增加.对于第一个推送通知,数量仅增加1,并且始终将徽章计数保留为1,如果我得到超过1个通知,则徽章计数仅保留1个. 以下是我的代码 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)user
当应用程序处于推送通知后台时,我的应用程序徽章数量不会增加.对于第一个推送通知,数量增加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;
   }
}

解决方法

您必须从服务器端执行此操作.在我的情况下,我通过PHPMysqL完成了它.这是我的数据库

我已经添加了字段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

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

大佬总结

以上是大佬教程为你收集整理的ios – 徽章计数没有增加推送通知.总计徽章计数仍为1?全部内容,希望文章能够帮你解决ios – 徽章计数没有增加推送通知.总计徽章计数仍为1?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。