在iOS10 - Swift 3中添加自定义本地通知

7

我正在尝试添加自定义本地通知,但只能得到带有我的操作的默认通知:

enter image description here

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

enter image description here

我是一名有用的助手,可以为您翻译文本。
我有一个扩展程序,其中UNNotificationExtensionCategory设置为awesomeNotification(在Info.plist中)。 此外,该扩展程序的基础是来自的通知内容模板。
在我的应用委托中,我有以下内容:
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+ 上运行时出现非常奇怪的行为: 在此输入图片描述


你的意思是你需要在通知中自定义视图,对吗? - LC 웃
是的,那就是我想要使用的。 - Bjarte
我一直在尝试同样的事情,但是即使在6s上也无法显示通知?你能把你的项目放到Git上吗? - LC 웃
我只能在6s上看到自定义的绿色区域。 - Bjarte
不知道为什么我做不到..运气不好..再努力试试看 - LC 웃
1个回答

5

更新:自iOS 10 beta 2起,富通知也适用于不支持3D Touch的设备。下拉常规通知即可查看。

请确保您正在iPhone6s / iPhone6s plus模拟器/设备上进行测试,它似乎不能在不支持3D Touch的设备上使用。

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


这太奇怪了...我本来期望它可以在iPhone SE或其他设备上运行,限制它只在那些设备上运行看起来非常奇怪。我还期望我的内容视图可以接管整个视图,但它仍然显示底部的库存...哦,算了! - Bjarte
我本来也期望能够解决这个问题,但最终却碰了壁。看起来这个问题会在其他型号上得到解决:http://www.macrumors.com/2016/06/14/rich-notifications-without-3d-touch/ - Eirik Berntsen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接