【问题标题】:scheduleLocalNotification in iOS 10 device using iOS 9.3 SDK when in foreground在前台使用 iOS 9.3 SDK 的 iOS 10 设备中的 scheduleLocalNotification
【发布时间】:2016-09-09 15:27:09
【问题描述】:

当应用程序在前台时,我在使用 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 中访问此内容。

【问题讨论】:

    标签: push-notification ios9 ios10


    【解决方案1】:

    这似乎只是 iOS 10 中新的预期处理方式。如果您在应用程序处于前台时发布通知,它将调用 didReceiveLocalNotification 委托,但不会将其发布到通知中心托盘。

    为了解决这个问题,我现在将所有收到的通知存储在一个静态数组中,然后当应用程序进入后台时,我将安排在 5 秒内发送通知,只是为了给应用程序足够的时间来完全转到背景。

    创建/附加通知:

    /**
     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)
    }
    

    最终发送,调用applicationDidEnterBackgroundapplicationWillTerminate

    /**
     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 行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2012-05-04
      • 2019-03-09
      • 1970-01-01
      • 2018-10-26
      相关资源
      最近更新 更多