最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中添加app消息通知桌面图标显示红色数字的支持

Swift crifan 2773浏览 0评论

在折腾:

[已解决]Swift中提醒用户是否授权发送通知消息时app名字为(null)

期间,顺带发现了,如何添加支持,消息通知时的app的桌面图标添加红色数字badge

Registering, Scheduling, and Handling User Notifications

Local and Remote Notifications in Depth

iOS Programming Tutorial: Adding Local Notification to iPhone App

用代码:

        //enable notification
        let notifSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge],
            categories: nil)
        application.registerUserNotificationSettings(notifSettings)
        //reset badge number
        application.applicationIconBadgeNumber = 0
    func sendNotification(newMessage:Message){
        if newMessage is TextMessage {
            let textMsg = newMessage as! TextMessage
           
            var notifBody = textMsg.text
            if !textMsg.senderName.isEmpty {
                notifBody = textMsg.senderName + ": " + notifBody
            }
            let localNotif = UILocalNotification()
            localNotif.fireDate = NSDate(timeIntervalSinceNow: 1)
            localNotif.timeZone = NSTimeZone.defaultTimeZone()
            localNotif.alertBody = notifBody
            print("before localNotif applicationIconBadgeNumber=\(UIApplication.sharedApplication().applicationIconBadgeNumber)")
            localNotif.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
            print("after  localNotif applicationIconBadgeNumber=\(UIApplication.sharedApplication().applicationIconBadgeNumber)")
            UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
        }
    }

去测试:

但是:

点击app图标进入app后,桌面的图标中的badge还是没变化,并没有清除掉为0

转载请注明:在路上 » [已解决]swift中添加app消息通知桌面图标显示红色数字的支持

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.193 seconds, using 22.05MB memory