iOS 10 台设备在前台使用 iOS 9.3 SDK 中的 scheduleLocalNotification
scheduleLocalNotification in iOS 10 device using iOS 9.3 SDK when in foreground
当应用程序处于前台时,我在使用 iOS 9.3 SDK 时在 iOS 10 设备中安排本地通知时遇到问题。我们的应用程序的设计方式是,如果我们收到远程通知并且应用程序当前处于前台,我们会将其重新发布到本地,以便用户在将应用程序置于后台时仍然可以看到通知。这适用于 iOS 9 台设备,但不适用于 iOS 10.
转发码:
dispatch_async(dispatch_get_main_queue(), {
let localNotification = UILocalNotification()
localNotification.userInfo = userInfo;
localNotification.fireDate = NSDate()
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
if let apnsPayload = userInfo["aps"] {
if let alert = apnsPayload["alert"] as? NSDictionary {
if let message = alert["body"] as? String {
localNotification.alertBody = message
}
}
if let category = apnsPayload["category"] {
localNotification.category = category as? String
}
}
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
})
我相信 scheduleLocalNotification
的重新发布工作有些正确,因为我可以看到我的 didReceiveLocalNotification
委托在 AppDelegate
中被调用。但是,如果我在将应用程序置于前台或后台时下拉通知下拉菜单,则没有通知。
还有其他人 运行 遇到过这个问题吗?我已经看到关于如何使用 iOS 10 UNUserNotificationCenter
的帖子丢失了,但我无法在 iOS 9.3 SDK 中访问它。
这似乎只是 iOS 10 中新的预期处理方式。如果您 post 在应用程序处于前台时发出通知,它将调用 didReceiveLocalNotification
委托,但是它不会 post 它到通知中心托盘。
为了解决这个问题,我现在将所有收到的通知存储在一个静态数组中,然后当应用程序进入后台时,我会安排通知在 5 秒内发送,只是为了给应用程序足够的时间来完成转到后台。
Creating/Appending 条通知:
/**
Takes a given Dictionary that should correspond to a Remote notification and reposts it (at current time)
as a Local Notification. If the app is in the foreground, then no banner, alert, or sound will play, but the
notification should appear in the Notification Center.
- parameter userInfo: Dictionary that should correspond to a Remote notification.
*/
static func repostLocalNotificationUsingRemote(userInfo: Dictionary<NSObject, AnyObject>) {
let localNotification = UILocalNotification()
localNotification.userInfo = userInfo;
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
if let apnsPayload = userInfo["aps"] {
if let alert = apnsPayload["alert"] as? NSDictionary {
if let message = alert["body"] as? String {
localNotification.alertBody = message
}
}
if let category = apnsPayload["category"] {
localNotification.category = category as? String
}
}
// Add notification to queue
localNotificationQueue.append(localNotification)
}
最终发送,调用 applicationDidEnterBackground
和 applicationWillTerminate
:
/**
Posts all pending notifications that were received while app was in foreground.
*/
static func postPendingLocalNotificationIfAny() {
for localNotification in localNotificationQueue {
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
localNotificationQueue.removeAll();
}
理想情况下,我们将更新到 iOS 10 SDK 并使用 UNUserNotificationCenter
在应用程序中显示通知,但在此之前,此更改匹配当前的 iOS 9 行为。
当应用程序处于前台时,我在使用 iOS 9.3 SDK 时在 iOS 10 设备中安排本地通知时遇到问题。我们的应用程序的设计方式是,如果我们收到远程通知并且应用程序当前处于前台,我们会将其重新发布到本地,以便用户在将应用程序置于后台时仍然可以看到通知。这适用于 iOS 9 台设备,但不适用于 iOS 10.
转发码:
dispatch_async(dispatch_get_main_queue(), {
let localNotification = UILocalNotification()
localNotification.userInfo = userInfo;
localNotification.fireDate = NSDate()
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
if let apnsPayload = userInfo["aps"] {
if let alert = apnsPayload["alert"] as? NSDictionary {
if let message = alert["body"] as? String {
localNotification.alertBody = message
}
}
if let category = apnsPayload["category"] {
localNotification.category = category as? String
}
}
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
})
我相信 scheduleLocalNotification
的重新发布工作有些正确,因为我可以看到我的 didReceiveLocalNotification
委托在 AppDelegate
中被调用。但是,如果我在将应用程序置于前台或后台时下拉通知下拉菜单,则没有通知。
还有其他人 运行 遇到过这个问题吗?我已经看到关于如何使用 iOS 10 UNUserNotificationCenter
的帖子丢失了,但我无法在 iOS 9.3 SDK 中访问它。
这似乎只是 iOS 10 中新的预期处理方式。如果您 post 在应用程序处于前台时发出通知,它将调用 didReceiveLocalNotification
委托,但是它不会 post 它到通知中心托盘。
为了解决这个问题,我现在将所有收到的通知存储在一个静态数组中,然后当应用程序进入后台时,我会安排通知在 5 秒内发送,只是为了给应用程序足够的时间来完成转到后台。
Creating/Appending 条通知:
/**
Takes a given Dictionary that should correspond to a Remote notification and reposts it (at current time)
as a Local Notification. If the app is in the foreground, then no banner, alert, or sound will play, but the
notification should appear in the Notification Center.
- parameter userInfo: Dictionary that should correspond to a Remote notification.
*/
static func repostLocalNotificationUsingRemote(userInfo: Dictionary<NSObject, AnyObject>) {
let localNotification = UILocalNotification()
localNotification.userInfo = userInfo;
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
if let apnsPayload = userInfo["aps"] {
if let alert = apnsPayload["alert"] as? NSDictionary {
if let message = alert["body"] as? String {
localNotification.alertBody = message
}
}
if let category = apnsPayload["category"] {
localNotification.category = category as? String
}
}
// Add notification to queue
localNotificationQueue.append(localNotification)
}
最终发送,调用 applicationDidEnterBackground
和 applicationWillTerminate
:
/**
Posts all pending notifications that were received while app was in foreground.
*/
static func postPendingLocalNotificationIfAny() {
for localNotification in localNotificationQueue {
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
localNotificationQueue.removeAll();
}
理想情况下,我们将更新到 iOS 10 SDK 并使用 UNUserNotificationCenter
在应用程序中显示通知,但在此之前,此更改匹配当前的 iOS 9 行为。