实现在接受到PN时更新徽章图标。
我试过添加代码但是在应用关闭后无法实现,只有在应用前台运行时才能实现。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[application setApplicationIconBadgeNumber:100];
return YES;
}
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[ [UIApplication sharedApplication] setApplicationIconBadgeNumber: 30];
}
在应用关闭后更新应用的徽章图标的操作是不可能的,因为当应用程序处于关闭状态时,应用程序的代码是不会运行的。
但是,可以在收到远程通知时更新应用的徽章图标。这可以通过将以下代码添加到 didReceiveRemoteNotification 方法中来实现:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Update the badge icon
[application setApplicationIconBadgeNumber:application.applicationIconBadgeNumber + 1];
}
请注意,您可能需要调整代码以使用您想要的徽章图标值。