点击时清除横幅通知

Posted

技术标签:

【中文标题】点击时清除横幅通知【英文标题】:Clear Banner Notification When Tapped 【发布时间】:2015-03-21 04:23:50 【问题描述】:

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

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


【问题讨论】:

【参考方案1】:

你必须使用以下来删除所有通知

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

【讨论】:

这不起作用。点击横幅后,通知仍然出现在通知中心。 从所有地方删除以下内容:[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; [[UIApplication sharedApplication] cancelLocalNotification:notification]; 点击Banner后,通知中心仍然出现。 您在横幅通知一发送就点击它?我不是在谈论在通知中心点击通知,那些工作正常。 这将清除所有通知。它不仅会清除选定的通知。【参考方案2】:

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

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

     [application cancelLocalNotification:notification];
    

【讨论】:

这不起作用。点击横幅后,通知仍会出现在通知中心。我忘了补充说我已经实现了这个方法。

以上是关于点击时清除横幅通知的主要内容,如果未能解决你的问题,请参考以下文章

如何删除徽章但不删除所有通知?

收到推送通知时清除徽章

关闭推送通知横幅

如何从点击通知列表中清除单个通知?

android 如何让通知不被清除或者点击后不消失

如何清除从 Android 中点击通知打开的先前活动?