How to update the badge number in IOS push notification?

霸气de小男生 提交于 2019-12-06 00:15:01

You are supposed to send the badge number in the payload of the push notification. Your server should set the badge count. This way the badge count will be updated regardless of whether the app is running or not running (and regardless of whether the user taps the notification to open the app or not). Your server should know what number to send.

And as to how your server is supposed to know what badge number to send - I don't know your app's logic, but lets take an email application as an example - suppose you want the badge number to show how many unread messages exist. The app should notify the server whenever a message is read, so the server can maintain the correct badge number for each user. This way, even if the user has multiple ways to access the data (iPhone app, iPad app, desktop browser, etc...), the server would know the current badge count and each device would show the correct badge count.

The application.applicationIconBadgeNumber = 0 is meant to clear the badge once the application is opened. It should be done in both didReceiveRemoteNotification and didFinishLaunchingWithOptions, so that the badge number will always be cleared when the application is launched, or when the push notification is handled by the app.

You can do something like this. Here I am using AWS Lambda and AWS SNS.

var params = {
'TargetArn' : 'Your Target Device',
'MessageStructure' : 'json',
'Message' : JSON.stringify({
  'default' : 'New Request from User',
  'APNS_SANDBOX' : JSON.stringify({
    'aps' : { 'alert' : 'New Request from User', 'badge':1,'category' : 'MESSAGE_CATEGORY' },
    'badge' : '1',
    'sound' : 'default',
    'attribute1' : "attribute 1 value",
    'attribute2' : "attribute 2 value",
    'attributeN' : "attribute N value"
  })
})
};

sns.publish(params, function(err, data) {
    if (err) {
        console.log(err.stack);
        return;
    }
    console.log('push sent');
    console.log(data);
    context.done(null, 'Function Finished!');  
});

ok I figure out how to use the badge number with Amazon SNS, if you publish your notification with PHP, then you can implement the badge like this

                        $sns->publish(array(
                        'MessageStructure' => 'json',
                        'TargetArn' => $endpointArn,
                        'Message' => json_encode(array(
                            'APNS_SANDBOX' => json_encode(array(
                                'aps' => array(
                                    'alert' => $push_message,
                                    'badge' => 1
                                )
                            ))
                        )),
                    ));

Just don't forget to set the badge to zero at your appdelegate.m , in the onReceive, onLaunchwithoption and onCompletefromBackground (Can't remember the excat spelling)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!