当应用程序处于前台时远程通知到达时,iOS 9和10+本地通知

时间:2017-09-17 14:31:26

标签: ios swift notifications

我的应用程序当前接收远程通知,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等),但它确实出现在通知中心。

我错过了什么吗?

我最终需要实施的一些事情:

  • 通知中的操作按钮,例如取消和接受朋友请求,适用于所有情况(前景,后台和非活动)
  • 应用程序完全未运行(已终止/未运行)时的通知

0 个答案:

没有答案