相关疑难解决方法(0)

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

我有一个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已经运行了.但通知仍然在通知中心挂起.为什么?

cocoa-touch objective-c apple-push-notifications

109
推荐指数
5
解决办法
11万
查看次数