简体   繁体   English

如何增加推送通知徽章iPhone

[英]How to Increment the Push notification Badge iPhone

Is it possible to increment the badge value on receiving the notification. 是否可以在收到通知时增加徽章值。 OR Should I send the count as payload ? 还是应该将计数作为有效载荷发送?

If i am sending the badge value as "1" every time, how could i increment the badge-value in the icon of the app if the app is not open. 如果我每次都将徽章值发送为“ 1”,如果应用程序未打开,我如何在应用程序的图标中增加徽章值

I used this code but doesn't work. 我使用了此代码,但不起作用。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; 
}

If you want to change the icon badge automatically use the following code. 如果要自动更改图标徽章,请使用以下代码。

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

    application.applicationIconBadgeNumber = 0;
    NSLog(@"userInfo %@",userInfo);

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    [application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]];

    NSLog(@"Badge %d",[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]);

}

We also need to change the php file. 我们还需要更改php文件。 So we can get the change the icon badge automatically 这样我们就可以自动获取更改图标徽章

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default',
    'id' => '135',
    'badge' => 8
    );

Since push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification. 由于推送通知是由iOS处理的,而不是由您的应用处理的,因此您无法在收到推送通知时更改应用程序标志。

But you can send the badge number in the payload of the push notification, but the you will have to do the calculation server side. 但是您可以在推送通知的有效负载中发送徽章编号,但是您将必须在计算服务器端进行操作。

You should read :Local and Push Notification Programming Guide and specially the The Notification Payload. 您应该阅读:Local and Push Notification Programming Guide,尤其是The Notification Payload。

The payload could look like this: 有效负载可能如下所示:

{ "aps" : { "alert" : "You got your emails.", "badge" : 9 } } Now the app application badge icon will show 9. {“ aps”:{“ alert”:“您收到了电子邮件。”,“徽章”:9}}现在,应用程序应用程序徽章图标将显示9。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM