在折腾:
[已解决]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消息通知桌面图标显示红色数字的支持