点击时清除横幅通知

时间:2015-03-21 04:23:50

标签: ios objective-c iphone notifications push-notification

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

- (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];

}

2 个答案:

答案 0 :(得分:2)

您必须使用以下内容删除所有通知

- (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];
    }

答案 1 :(得分:0)

您可以实现此委托方法:

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