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

dhr*_*hrm 109 cocoa-touch objective-c apple-push-notifications

我有一个iOS应用程序,其中发送了一些推送通知.我的问题是,在点击后,消息/通知会保留在iOS中的通知中心.下次应用程序打开时如何在通知中心删除我的应用程序通知?

我遇到过人们调用setApplicationIconBadgeNumber零值来清除通知的帖子.这对我来说似乎很奇怪,所以我相信也许存在另一种解决方案?

EDIT1:

我在清除通知时遇到了一些问题.请在这里查看我的代码:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我正在通过Xcode运行App.当应用程序最小化并且我使用通知中心中的通知启动应用程序时,我可以在日志didReceiveRemoteNotification中看到,调用并使用我可以看到的断点,clearNotifications已经运行了.但通知仍然在通知中心挂起.为什么?

Pat*_*ini 156

很可能因为通知中心是一个相对较新的功能,Apple并不一定想要推出一个全新的清除通知范例.因此,他们多[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];用于清除所述通知.这可能看起来有点奇怪,Apple可能会在未来提供更直观的方式来做到这一点,但目前它是正式的方式.

我自己,我用这个片段:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Run Code Online (Sandbox Code Playgroud)

从来没有从通知中心清除所有应用程序的通知.


ADA*_*DAM 119

只是为了扩展pcperini的答案.正如他所提到的,您需要将以下代码添加到您的application:didFinishLaunchingWithOptions:方法中;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Run Code Online (Sandbox Code Playgroud)

需要增加然后递减徽章在你的application:didReceiveRemoteNotification:方法,如果你想,这样当用户从按下的通知应用程序进入你从信息中心清除消息信息中心将同样明显,即;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Run Code Online (Sandbox Code Playgroud)


ber*_*ert 20

在applicationDidBecomeActive中添加对clearNotifications的调用也是有意义的,这样如果应用程序在后台并返回,它也将清除通知.

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self clearNotifications];
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*ill 15

iOS 10更新(Swift 3)

要清除iOS 10应用中的所有本地通知,您应使用以下代码:

import UserNotifications

...

if #available(iOS 10.0, *) {
    let center = UNUserNotificationCenter.current()
    center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
    center.removeAllDeliveredNotifications() // To remove all delivered notifications
} else {
    UIApplication.shared.cancelAllLocalNotifications()
}
Run Code Online (Sandbox Code Playgroud)

此代码处理iOS 10.x和所有先前版本的iOS的本地通知的清除.您将需要import UserNotificationsiOS 10.x代码.


osp*_*spr 9

如果您有待处理的计划本地通知,并且不想用于cancelAllLocalNotifications在通知中心中清除旧通知,您还可以执行以下操作:

[UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
Run Code Online (Sandbox Code Playgroud)

看来,如果您将scheduledLocalNotifications其设置为清除Notification Center中的旧版本,并将其设置为自身,则会保留待处理的本地通知.