【问题标题】:Cannot set badge number after a push notification arrival推送通知到达后无法设置徽章编号
【发布时间】:2018-09-04 20:42:48
【问题描述】:

在我正在开发的应用程序(iOS 11)中,更新我们使用的徽章编号:

UIApplication.shared.applicationIconBadgeNumber = number

应用程序使用提供的值更新徽章编号,没有问题。

然后……

推送通知通过“徽章”键到达,应用程序使用推送通知值更新徽章编号,但是从那里使用 applicationIconBadgeNumber 的所有更新将不再起作用。知道为什么吗?

【问题讨论】:

    标签: ios swift push-notification badge


    【解决方案1】:

    您需要在AppDelegate 中实现 UserNotifications。

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    

    didFinishLaunchingWithOptions中使用以下代码

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
            if error != nil {
                // Your Code 
            }
        }
        return true
    
    }
    

    徽章更新需要使用如下代码

    let alert = UNMutableNotificationContent() 
    alert.badge = 10 //your badge count
    

    【讨论】:

    • 感谢您的回答。我已经测试了 UNMutableNotificationContent() 代码,现在我根本无法更新徽章编号,只能通过删除通知。
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2015-06-14
    相关资源
    最近更新 更多