在iOS 10-Swift 3中添加本地通知

亮度:

因此,我一直在尝试向新的UNUserNotificationCenter添加通知,但似乎没有收到。

我的视图控制器有一个动作:

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

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

    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest.init(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")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization([.alert, .sound]) { (granted, error) in
    }
}

Notification Content Extension在项目中也有一个,但似乎根本没有触发任何想法,我缺少什么?我正在尝试用户文档中的示例,但并没有告诉我更多信息,或者我错过了它。

此处:https : //developer.apple.com/reference/usernotifications/unmutablenotificationcontent

另外:https : //developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotifications

编辑:

因此,将应用程序置于后台就可以了。

笑艾尼(Anish Parajuli):

您需要注册通知...我尝试过,并且可以正常工作。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization([.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        return true
    }

编辑:您无需将应用置于后台即可从iOS 10开始显示通知。

使用下面的回调将通知配置为出现在前台。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

是一个示例项目。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swift 3 IOS 9和IOS 10核心数据

在IOS 10(Swift 3,Xcode 8)中检测GSM呼叫状态并从后台状态通知

在一周的特定日期重复本地通知(Swift 3 IOS 10)

iOS Swift本地通知未“弹出”

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

注册推送通知Swift 3 + iOS 10

ios10,Swift 3和Firebase推送通知(FCM)

iOS10:从本地通知中点击操作不会将应用程序置于前台

如何获取从iOS 10和Swift 3中的Firebase控制台发送的推送通知的主体?

NSKeyedArchiver.unarchiveObject Swift 3 iOS 10

如何在iOS 10中安排本地通知(objective-c)

本地通知未在ios10中触发

Swift 3 / iOS 10泄漏

iOS 10中新的UserNotifications仍然有〜64个本地通知限制?

iOS 10中不显示本地通知

为什么在iOS 10中前台不会触发我的本地通知?

手动设置时间和日期时,不会触发iOS 10中的重复每日本地通知?

等待直到使用removePendingNotificationRequests iOS 10 swift 3删除来自UNUserNotificationCenter的本地通知

从iOS 10中的本地通知启动应用

当应用程序处于前台Swift 4 iOS 11时获取本地通知

无法查看iOS 10本地通知的操作按钮

Firebase setValue在IOS 10,Swift 3中不起作用

在Swift 3的锁定屏幕中清除本地通知

Swift:将本地通知转换为用户通知IOS 10

如何在 iOS 10(Swift) 中取消特定的重复本地通知并将其重新安排到下周

针对特定日期 iOS 10 重复本地通知

iOS 中的本地通知(目标 C)

在 Swift 中更改本地通知的文本

基于用户在 ToDo DatePicker (Swift & SwiftUI) 中设置的日期时间的 iOS 本地通知