【问题标题】:Badge Count is not increasing for push notification.always badge count remains 1?推送通知的徽章计数没有增加。徽章计数总是保持 1?
【发布时间】:2013-09-29 16:32:07
【问题描述】:

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

【问题讨论】:

  • 我看到很多关于推送通知和证书的类似问题,只是想知道这是来自哪个类。
  • 您的服务器向 APNS 发送什么有效负载?
  • [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];我正在使用上述有效载荷
  • 我的意思是你的服务器正在发送的数据,它为徽章设置了什么值?
  • aps = { alert = "第三次测试";徽章 = 1; sound = "sound.caf"; };每次我得到这个响应表单服务器。但是我在点击通知后得到这个

标签: ios xcode push-notification apple-push-notifications badge


【解决方案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); 
        ...

我还制作了另一个 api 来制作在

中调用的 badgcount 0
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

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

【讨论】:

    【解决方案2】:

    你说你的有效载荷是:

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

    由于您总是发送1 以获取徽章计数,因此您的应用会显示该徽章计数。徽章计数不是递增的。如果您想查看大于 1 的徽章计数,您的服务器应将大于 1 的值放入您的有效负载中。

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

    【讨论】:

    • [UIApplication sharedApplication].applicationIconBadgeNumber =[UIApplication sharedApplication].applicationIconBadgeNumber+1;我可以通过这条线增加徽章数量,但是。这里有什么问题是我在选择通知后获得徽章计数我需要在不选择通知的情况下获取它
    • 这是您不应该在应用程序中以编程方式增加徽章计数的另一个原因 - 您的代码仅在用户点击通知后才会执行。在应用中设置徽章只能在用户查看通知后用于清除它(将其设置为零)。
    • @user2230971 正如我在回答中建议的那样,从您的服务器发送一个 > 1 的徽章。让您的服务器维护每台设备的徽章计数,并决定在推送通知中发送的徽章计数。
    • @user2230971 您的设备可以向您的服务器发送一个请求,报告(例如)已读通知的数量。
    • 您需要在阅读通知时向您的服务器发送一些内容
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多