[英]How to update notification badge number in iOS's home screen when received push message?
[英]How to update the badge number in IOS push notification?
我最近使用 Amazon SNS 為我的 IOS 應用程序推送通知。
效果很好,我遇到的唯一問題是當我收到通知時,徽章編號不會更新,這是我的實現方式:
首先我按照這里的示例https://aws.amazon.com/articles/9156883257507082這里是教程中的示例代碼。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
NSLog(@"%@",msg);
[[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Register for push notification
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
if(launchOptions!=nil){
NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
NSLog(@"%@",msg);
[[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
}
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
Message_BoardViewController *boardViewController = [Message_BoardViewController new];
UINavigationController *navigationController = [UINavigationController new];
navigationController.navigationBar.translucent = NO;
[navigationController pushViewController:boardViewController animated:NO];
[boardViewController release];
self.window.rootViewController = navigationController;
[navigationController release];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self.window makeKeyAndVisible];
// Logging Control - Do NOT use logging for non-development builds.
#ifdef DEBUG
[AmazonLogger verboseLogging];
#else
[AmazonLogger turnLoggingOff];
#endif
[AmazonErrorHandler shouldNotThrowExceptions];
return YES;
}
如您所見,從教程代碼中可以看出
application.applicationIconBadgeNumber = 0;
所以很明顯它每次都會變成0。
================================================== ====
我想知道更新徽章編號的標准方法是什么?
哪個方法是正確的?
1) 通過像這樣的編程application.applicationIconBadgeNumber = 0;
2)或者像這樣來自服務器有效載荷?
$body = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);
================================================== ======
但是,我發現每個 apporach 都有障礙,對於 1),當應用程序處於后台時didReceiveNotification
不會觸發,因此我無法執行諸如application.applicationIconBadgeNumber++;
更新徽章編號。
對於 2) ,Amazon SNS 服務僅返回
$body = array(
'alert' => $message,
);
以及服務器如何知道徽章編號並將其添加到服務器有效負載中,似乎我仍然需要在didReceiveNotification
中將更新徽章編號發布到亞馬遜並將其添加到有效負載中。 但同樣,它不會在后台調用。
對不起,IOS 編程新手,有經驗的程序員可以指導我通過推送通知實現徽章編號更新嗎? 謝謝。
您應該在推送通知的有效負載中發送徽章編號。 您的服務器應該設置徽章計數。 這樣,無論應用程序是否正在運行(也無論用戶是否點擊通知以打開應用程序),徽章計數都會更新。 您的服務器應該知道要發送的號碼。
至於您的服務器應該如何知道要發送的徽章編號 - 我不知道您的應用程序的邏輯,但讓我們以電子郵件應用程序為例-假設您希望徽章編號顯示存在多少未讀消息。 應用程序應該在讀取消息時通知服務器,以便服務器可以為每個用戶維護正確的徽章編號。 這樣,即使用戶有多種方式訪問數據(iPhone 應用程序、iPad 應用程序、桌面瀏覽器等),服務器也會知道當前的徽章計數,並且每個設備都會顯示正確的徽章計數。
application.applicationIconBadgeNumber = 0
是為了在應用程序打開后清除徽章。 它應該在didReceiveRemoteNotification
和didFinishLaunchingWithOptions
,以便在應用程序啟動時或應用程序處理推送通知時始終清除徽章編號。
你可以做這樣的事情。 我在這里使用 AWS Lambda 和 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!');
});
好的,我想出了如何在 Amazon SNS 中使用徽章編號,如果您使用 PHP 發布通知,那么您可以像這樣實現徽章
$sns->publish(array(
'MessageStructure' => 'json',
'TargetArn' => $endpointArn,
'Message' => json_encode(array(
'APNS_SANDBOX' => json_encode(array(
'aps' => array(
'alert' => $push_message,
'badge' => 1
)
))
)),
));
只是不要忘記在您的 appdelegate.m 、 onReceive、onLaunchwithoption 和 onCompletefromBackground 中將徽章設置為零(不記得 excat 拼寫)
請注意,此處要識別的關鍵是您發送到 AWS 的 SNS 服務的 JSON 中的屬性名稱。 在您的開發環境中,您將通過 APNS 沙箱發送,因此您的 JSON 將類似於:
{
"default": "Test push-to-token notification.",
"APNS_SANDBOX":"{\"aps\":{\"alert\":\"Test push-to-token notification.\",\"sound\":\"default\",\"badge\": 1}}",
"GCM":"{\"data\":{\"body\":\"Test push-to-token notification.\",\"action_button_text\":\"OK\",\"sound\":\"default\"}}"
}
當您進入生產環境時,您需要將"APNS_SANDBOX"
更改為"APNS"
。 所以像:
{
"default": "Test push-to-token notification.",
"APNS":"{\"aps\":{\"alert\":\"Test push-to-token notification.\",\"sound\":\"default\",\"badge\": 1}}",
"GCM":"{\"data\":{\"body\":\"Test push-to-token notification.\",\"action_button_text\":\"OK\",\"sound\":\"default\"}}"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.