Remove “remote notifications” from notification ce

2019-08-26 11:38发布

问题:

I have the following issue: "My app receive some remote notifications from an own server just to show to the user some practical information. I am not using a icon badge, because I don't need it. If the application user touch the remote notification from the iOS Notification Center my application can catch it without any problem, I receive the options from application:didFinishLaunchingWithOptions: or, if the application is open I catch the remote notification with application:didReceiveRemoteNotification: selector. But, now I want to remove these notifications from iOS Notification Center because It is just a message and I have been looking for the answer in another posts and I've tried these solutions in my app and they don't work"

Some solutions were the next:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  
    [application cancelAllLocalNotifications];  
    application.applicationIconBadgeNumber = 0;  
    ...  
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    ...  
    if (launchOptions) {  
        [application cancelAllLocalNotifications];  
        application.applicationIconBadgeNumber = 0;  
    }  
    ...  
}

And the remote notification still in the iOS Notification Center, how can I remove from that place without any tricky code or is an iOS SDK issue? I don't think that an issue was possible because Tweetbot app remove its remote notifications from iOS Notification Center after you enter to the app.

Regards!

回答1:

With introduction of UNUserNotificationCenter for iOS 10 and above, it is now possible to remove few or all remote notifications of your app.

UNUserNotificationCenter documentation

With a shared singleton instance of this class, it is possible to manage delivered remote notifications on the device. Specifically, the following methods can be used: func removeDeliveredNotifications(withIdentifiers: [String]) if you want to remove specific notification of your app, OR func removeAllDeliveredNotifications() to remove all notifications of your app.



回答2:

First of all make sure you haven't set badge notification to be off in the control panel (I noticed that if the badge has a number to begin with, then badging is turned off in the control panel, it cannot be set to 0).

If its not turned off then in addition to setting applicationIconBadgeNumber to 0, also try canceling all local notifications (even if you haven't queued any, if you have first get a list of them, then cancel them, then register them back again). Yes clearing local notifications can have an effect on being able to clear the badge number for remote notifications.