ios - 点击时清除横幅通知

标签 ios objective-c iphone notifications push-notification

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

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

关于ios - 点击时清除横幅通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179232/

相关文章:

iphone - NSLocale - 查找 CountryCode 然后使用它来部署代码

IOS 搜索标签名称补全气泡

iphone - 版本更新时的数据备份。应用商店

iphone - 如何将 CoreImage 过滤器应用于 View ?

ios - 自定义 UILabel 类 iOS

iphone - 在 iphone 设备上测试应用程序项目

ios - 将字符串拆分为 Objective-C 中的内容页面

ios - UITableViewCell 中的行仅显示来自 USGS 的第一个索引数组

objective-c - 如何从 View Controller 中 resignFirstResponder

ios - 如何在 iOS 中的 TableView 中实现下拉菜单