6

タブバー アイテムのバッジ通知と同様に、アプリ アイコンでバッジ通知を取得するにはどうすればよいでしょうか。新しいメッセージを通知するためにこれが必要です。

4

2 に答える 2

18

次のように、アプリ アイコンのバッジ番号を設定できます。

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
于 2011-01-25T07:25:00.387 に答える
3

バッジ番号を PUSH メッセージで送信したい場合は、次のように PUSH を送信できます。

{"aps":{"alert":"My Push Message","sound":"default","badge",3}}

次に、AppDelegate に以下を追加します。

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

// This get's the number you sent in the push and update your app badge.
[UIApplication sharedApplication].applicationIconBadgeNumber = [[userInfo objectForKey:@"badge"] integerValue];

// Shows an alert in case the app is open, otherwise it won't notify anything
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Notification!"
                                              message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]  delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
[alertView show];    
}

迅速:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

// This get's the number you sent in the push and update your app badge.
UIApplication.shared.applicationIconBadgeNumber = (userInfo["badge"] as? NSNumber)?.intValue ?? 0

// Shows an alert in case the app is open, otherwise it won't notify anything
let alertView = UIAlertView(title: "New Notification!", message: (userInfo["aps"] as? [AnyHashable : Any])?["alert"], delegate: self, cancelButtonTitle: "OK", otherButtonTitles: "")
alertView?.show()
}
于 2013-01-26T02:11:56.173 に答える