iOS的应用程序:如何清除通知?

问题描述:

我已经在那里了一些推送通知发送到iOS应用程序。我的问题是,这些消息/通知停留在iOS的通知中心,然后之后窃听。我怎样才能删除通知我在通知中心在​​下一次应用程序打开的应用程序?

I've an iOS application where some Push Notification are sent to. My problem is, that the messages/notifications stays in the Notification Center in iOS after then are tapped. How can I remove a notification for my application in the Notification Center next time the application opens?

我遇到的职位来到那里的人们正在呼吁 setApplicationIconBadgeNumber 来一个零值来清除通知。这似乎很奇怪我,所以我认为,也许另一种解决方案的存在?

I came across posts where people are calling setApplicationIconBadgeNumber to a zero-value to clear the notifications. That's seems very weird to me, so I believe that maybe another solution exists?

EDIT1:

我有一些问题,清除通知。请在这里看到我的code:

I'm having some problems clearing the notifications. Please see my code here:

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            NSLog(@"Launched from push notification: %@", dictionary);

            [self clearNotifications];
        }
    }

    return YES;
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{    
    NSLog(@"Received notification: %@", userInfo);
    [self clearNotifications];
}

我用X code运行App。当应用程序被最小化,我使用的通知,并在通知中心的应用程序开始,我可以在日志中看到,该 didReceiveRemoteNotification 被调用,并使用断点,我可以看到,该 clearNotifications 已经跑了。但还是通知挂在通知中心。为什么呢?

I'm running the App through Xcode. When the App is minimized and I start the App using the notification in the Notification Center, I can see in the log, that the didReceiveRemoteNotification is called and using breakpoints I can see, that the clearNotifications has ran. But still the notification hangs in the Notification Center. Why?

最有可能的,因为通知中心是一个相对较新的功能,苹果公司并不一定要推一个全新的范式清算通知。所以,相反,他们的多用途 [UIApplication的sharedApplication] setApplicationIconBadgeNumber:0]; 清说通知。这似乎有点不可思议,而苹果可能会提供一个更直观的方式在将来做到这一点,但暂时这是官方的方式。

Most likely because Notification Center is a relatively new feature, Apple didn't necessarily want to push a whole new paradigm for clearing notifications. So instead, they multi-purposed [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; to clear said notifications. It might seem a bit weird, and Apple might provide a more intuitive way to do this in the future, but for the time being it's the official way.

我自己,我用这个片断:

Myself, I use this snippet:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

从未未能清除所有应用的通知,从通知中心。

which never fails to clear all of the app's notifications from Notification Center.