在ios 8(Swift 3)

时间:2017-02-12 08:10:47

标签: ios swift ios8 localnotification

我一直在搜索论坛,但无法为Swift 3找到一个好的答案。

我通过UIApplication.shared.scheduleLocalNotifiction(nofication)生成本地通知,但 .background activationMode 似乎无法正常工作。我了解当用户在此模式下点击此通知时,它应该将我的应用程序置于.background状态并执行我的代码而不将我的应用程序放在前台。

以下是我在控制器的viewDidLoad()中的代码(它是帖子中的复制粘贴):

let restartAction = UIMutableUserNotificationAction()
    restartAction.identifier = "xx"
    restartAction.isDestructive = false
    restartAction.title = "Restart"
    restartAction.activationMode = .background
    restartAction.isAuthenticationRequired = false


    let xAction = UIMutableUserNotificationAction()
    xAction.identifier = "xx1"
    xAction.isDestructive = false
    xAction.title = "Start"
    xAction.activationMode = .background
    xAction.isAuthenticationRequired = false

    let categoryIdentifier = "category.identifier"
    category.identifier = categoryIdentifier
    category.setActions([xAction,restartAction], for: .minimal)
    category.setActions([xAction, restartAction], for: .default)

    let categories = Set(arrayLiteral: category)
    let settings = UIUserNotificationSettings(types: [.alert, .sound], categories: categories)
    UIApplication.shared.registerUserNotificationSettings(settings)


    let localNotif = UILocalNotification()
    localNotif.alertBody = "testBody"
    localNotif.category = categoryIdentifier

    // Notification will be shown after 10 second (IMPORTANT: if you want to see notification you have to close or put app into background)
    localNotif.fireDate = NSDate().addingTimeInterval(20) as Date
    UIApplication.shared.scheduleLocalNotification(localNotif)

Appdelegate我有以下内容:

 func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    print("Local notification received 1")
    print (notification)
}

我的代码有两个问题(可能彼此相关):

  1. 在点击通知时,应用程序被置于前台,然后调用我的应用程序(:didReceieve :)。
  2. 我的通知中没有附加选项按钮。应该有2个按钮,用户可以点击"开始"和"重新启动"
  3. 我做错了什么?

    好的,上面的代码正在运行,但是在ios 10中你必须拉下通知以显示选项。与按钮在通知框中的图片不同

0 个答案:

没有答案
相关问题