打开应用程序后从通知中心删除推送

问题描述:

当我向我的应用程序发送 PUSH 通知时,它会停留在 iOS 5 的通知中心.如何从应用内的通知中心删除通知?

When I send out a PUSH notification to my app, it stays in the notification center in iOS 5. How can I remove the notification from the notification center from within the app?

清除徽章会清除通知中心的通知.

Clearing the badge clears the notifications from the notification center.

- (void)applicationDidBecomeActive:(UIApplication *)application {
   // Clear application badge when app launches
   [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

这仅在数字更改时有效.因此,要使其在徽章已经为零的情况下工作,请将其设置为某个值并在延迟后再次清除.

This only works if the number changes. So to make it work in case badge is already zero, set it to some value and clear it again after a delay.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    if([[UIApplication sharedApplication] applicationIconBadgeNumber] == 0)
       [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
   // Clear application badge when app launches
   [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

如果您以相同的方法设置和清除,则不起作用.

Doesn't work if you set and clear in the same method.