2015-05-21 39 views
10

當從iPhone屏幕頂部向下滑動時,是否有方法從通知橫幅中清除遠程通知。我嘗試了徽章數設置爲零:如何在應用程序中清除遠程通知?

application.applicationIconBadgeNumber = 0 

在委託didFinishLaunchingWithOptions,並didReceiveRemoteNotification,但它並沒有明確的通知。謝謝。

回答

10

您需要將IconBadgeNumber設置爲0並取消當前通知。我從來沒有在迅速做了,但我認爲它的代碼是波紋管:

application.applicationIconBadgeNumber = 0 
application.cancelAllLocalNotifications() 
+0

使用cancelAllLocalNotifications?這是遠程通知。感謝您的評論。 – Tedha

+0

OPS抱歉,我通常在同一時間同時使用兩個本地通知。 – Icaro

+0

或者,也許這也可以幫助http://stackoverflow.com/questions/10971825/remove-remote-notifications-from-notification-center – Icaro

0

我有遞增然後按順序遞減徽章計數爲它工作:

application.applicationIconBadgeNumber = 1 
application.applicationIconBadgeNumber = 0 
application.cancelAllLocalNotifications() 
1

斯威夫特3

在你AppDelegate.swift下的文件didFinishLaunchingWithOptions附加:

application.applicationIconBadgeNumber = 0 

在您的應用程序啓動時,這將刪除iOS徽章(應用程序圖標右上角的紅色圓圈)。

7

在iOS系統10中,上述的所有解決方案都貶值

'cancelAllLocalNotifications()' 在IOS 10.0被廢棄:使用UserNotifications框架的 - [UNUserNotificationCenter removeAllPendingNotificationRequests]

使用以下代碼來取消通知和復位徽章計數

對於iOS 10,夫特3.0

cancelAllLocalNotifications從iOS的10

@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]") 
open func cancelAllLocalNotifications() 

您必須添加此import語句過時,

import UserNotifications 

獲取通知中心。並執行操作如下面

application.applicationIconBadgeNumber = 0 // For Clear Badge Counts 
let center = UNUserNotificationCenter.current() 
center.removeAllDeliveredNotifications() // To remove all delivered notifications 
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled. 

如果你想刪除單個或多個具體的通知,您可以通過以下方法實現它。

center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"]) 

希望它有助於..!