【问题标题】:Add Custom Local Notification in ios10 - swift 3在 ios10 中添加自定义本地通知 - swift 3
【发布时间】:2016-06-15 07:57:19
【问题描述】:

我正在尝试添加自定义本地通知,但我仅通过我的操作获得库存通知:

我的故事板看起来像这样(标准模板):

我有一个将UNNotificationExtensionCategory 设置为awesomeNotification 的扩展名(在Info.plist 中)。此扩展的基础也是来自iOS - Application ExtensionNotification Content 模板。

在我的应用委托中,我有这个:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let center = UNUserNotificationCenter.current()

    let actions = [UNNotificationAction.init(identifier: "Hey", title: "Yo", options: UNNotificationActionOptions.foreground)]

    let category = UNNotificationCategory(identifier: "awesomeNotification", actions: actions, minimalActions: actions, intentIdentifiers: [], options: [])
    center.setNotificationCategories([category])

    center.requestAuthorization([.alert, .sound]) { (granted, error) in
    }

    return true
}

在主应用程序的视图控制器中,我有以下操作来触发它:

@IBAction func sendPressed(_ sender: AnyObject) {
    let content = UNMutableNotificationContent()

    content.categoryIdentifier = "awesomeNotification"
    content.title = "Hello"
    content.body = "What up?"
    content.sound = UNNotificationSound.default()

    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)

    // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request) { (error) in
        print(error)
    }
    print("should have been added")
}

编辑

所以它适用于 iPhone 6s/6s+,非常奇怪的行为:

【问题讨论】:

  • 你的意思是你需要在通知中自定义视图吗?
  • 是的,这就是我想要使用的。
  • 我一直在尝试相同的方法..但即使在 6 秒内也无法显示通知?您可以将您的项目放入 git 中吗?
  • 我只能看到6s的自定义绿区
  • 不知道为什么我不能..运气不好..再努力

标签: swift swift3 uilocalnotification ios10 unusernotificationcenter


【解决方案1】:

更新:从 iOS 10 beta 2 开始,丰富的通知也可以在 pre-3D touch 设备上使用。下拉常规通知即可查看。

确保您在 iPhone6s/iPhone6s plus 模拟器/设备上进行测试,它似乎不适用于预 3D 触控设备。

在 iPhone6 模拟器上,尝试单击并向下拖动您收到的股票通知,您应该会看到您的自定义 UI。

【讨论】:

  • 这太奇怪了……我原以为它可以在 iPhone SE 甚至其他设备上运行,但将其限制在这些设备上似乎很奇怪。我也期待我的内容视图取代视图,但它仍然显示下面的股票......哦,好吧!
  • 我也有同样的期待,并用这个确切的问题将我的头撞到了墙上。看起来它稍后会出现在其他模型上:macrumors.com/2016/06/14/rich-notifications-without-3d-touch
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
相关资源
最近更新 更多