2015-05-21 13 views
10

Có cách nào để xóa thông báo từ xa khỏi biểu ngữ thông báo khi vuốt xuống từ phía trên cùng của màn hình iPhone hay không. Tôi đã cố gắng thiết lập các số huy hiệu để zero:Làm thế nào để xóa thông báo từ xa trong ứng dụng của bạn?

application.applicationIconBadgeNumber = 0 

trong đoàn didFinishLaunchingWithOptionsdidReceiveRemoteNotification, nhưng nó không xóa thông báo. Cảm ơn.

Trả lời

10

Bạn cần đặt IconBadgeNumber thành 0 và hủy thông báo hiện tại. Tôi không bao giờ đã làm trong nhanh chóng nhưng tôi nghĩ rằng mã cho nó sẽ là như dưới đây:

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

Sử dụng cancelAllLocalNotifications? Đây là thông báo từ xa. Cảm ơn ý kiến. – Tedha

+0

Rất tiếc, tôi đã nhầm lẫn với các thông báo địa phương như tôi thường sử dụng cả hai cùng một lúc. – Icaro

+0

Hoặc có thể điều này cũng có thể giúp quá http://stackoverflow.com/questions/10971825/remove-remote-notifications-from-notification-center – Icaro

0

tôi phải tăng sau đó giảm giá trị đếm huy hiệu để cho nó làm việc:

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

Swift 3

Trong bạn AppDelegate.swift tập tin dưới didFinishLaunchingWithOptions add:

application.applicationIconBadgeNumber = 0 

Khi khởi chạy ứng dụng, thao tác này sẽ xóa huy hiệu iOS (vòng tròn màu đỏ ở góc trên cùng bên phải của biểu tượng ứng dụng).

7

Trong iOS 10, trên tất cả các giải pháp được khấu hao

'cancelAllLocalNotifications()' được tán thành trong iOS 10.0: Sử dụng UserNotifications Khung của - [UNUserNotificationCenter removeAllPendingNotificationRequests]

Sử dụng mã dưới đây để hủy thông báo và đặt lại Số lượng huy hiệu

Đối với iOS 10, Swift 3.0

cancelAllLocalNotifications bị phản đối từ iOS 10.

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

Bạn sẽ phải thêm tuyên bố nhập khẩu này, trung tâm thông báo

import UserNotifications 

Nhận. Và thực hiện thao tác như bên dưới

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. 

Nếu bạn muốn xóa một hoặc nhiều thông báo cụ thể, bạn có thể thực hiện theo phương thức dưới đây.

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

Hy vọng nó sẽ giúp .. !!

Các vấn đề liên quan