亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

守著一只汪 2019-10-05 14:41:54
我有一個iOS應用程序,其中向其中發送了一些推送通知。我的問題是,在點擊郵件/通知后,它們會留在iOS的通知中心中。下次打開應用程序時,如何在通知中心中為我的應用程序刪除通知?我碰到過有人呼吁setApplicationIconBadgeNumber零值清除通知的帖子。這對我來說似乎很奇怪,所以我相信也許存在另一種解決方案?編輯1:我在清除通知時遇到了一些問題。請在這里查看我的代碼:- (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];}我正在通過Xcode運行該應用程序。當最小化應用程序并使用通知中心中的通知啟動應用程序時,我可以在日志中看到didReceiveRemoteNotification調用了,并使用斷點看到了clearNotifications已運行。但是通知仍然掛在通知中心中。為什么?
查看完整描述

3 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

很可能是因為Notification Center是一個相對較新的功能,Apple并不一定要推動全新的范例來清除通知。因此,他們有多種用途[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];來清除上述通知。這似乎有點怪異,Apple將來可能會提供一種更直觀的方式來執行此操作,但目前這是官方方式。


我自己,使用以下代碼段:


[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];

[[UIApplication sharedApplication] cancelAllLocalNotifications];

永遠不會從通知中心清除該應用程序的所有通知。


查看完整回答
反對 回復 2019-10-05
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

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()

}

此代碼處理針對iOS 10.x和所有先前版本的iOS的本地通知的清除。您將需要import UserNotificationsiOS 10.x代碼。


查看完整回答
反對 回復 2019-10-05
  • 3 回答
  • 0 關注
  • 1053 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號