我的应用程序当前接收远程通知,AppDelegate将有效负载转发到应用程序的SWRevealViewController,当用户点击它时,该应用程序处理通知应该将用户发送到的位置。当应用程序处于后台或处于非活动状态时,这种粗略工作。
现在是前景案例。我尝试将此代码在收到远程通知并且应用程序位于前台时触发:
func displayLocalNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "iOS 10 title"
content.body = "iOS 10 body."
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default()
var dateComponents = DateComponents()
dateComponents.hour = 15
dateComponents.minute = 49
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
} else {
// ios 9
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5) as Date
notification.alertBody = "iOS 9 Body"
notification.alertAction = "iOS 9 Action"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}
}
我目前正在使用iOS 9测试iPhone 4s,并且通知不会出现在应用内(如Facebook和Whatsapp等),但它确实出现在通知中心。
我错过了什么吗?
我最终需要实施的一些事情: