点击时清除横幅通知

Clear Banner Notification When Tapped

我注意到,当我 select 从通知中心为我的应用程序推送通知时,该通知不再出现在通知列表中。但是,当我收到通知并立即点击横幅时,应用程序会正常打开,但当我下拉查看通知中心时,该通知仍然存在。我的委托中有以下推送处理代码:

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

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    //Presenting view controllers...

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    // Extract the notification data
    NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

    if(notificationPayload)
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];

    }
    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

    // when you tap on any of notification this delegate method will call...
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] cancelLocalNotification:notification];

}

您必须使用以下方法删除所有通知

- (void)applicationWillEnterForeground:(UIApplication *)application
    {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        [self clearNotifications];
    } 

- (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        [self clearNotifications];
    }

- (void)clearNotifications
    {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }

您可以实现这个委托方法:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
     [application cancelLocalNotification:notification];
}