本地通知 swift 3

Posted

技术标签:

【中文标题】本地通知 swift 3【英文标题】:Local Notifications swift 3 【发布时间】:2017-04-20 12:00:41 【问题描述】:

我正在尝试在单击按钮时发送通知。这是我的 viewController 中的代码:

@IBAction func getNotificationButtonPressed(_ sender: Any) 
    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Body"
    content.categoryIdentifier = "ident"
    content.sound = UNNotificationSound.default()

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)

    let request = UNNotificationRequest(identifier: "ident", content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()

    center.add(request)  (error : Error?) in
        if let theError = error 
            print(theError.localizedDescription)
         else 
            print ("success")
        
    

我也在 AppDelegate 中请求了使用通知的权限:

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound])  (granted, error) in
        // Enable or disable features based on authorization.
    
    application.registerForRemoteNotifications()

附:我已经导入了

import UserNotifications

在 AppDelegate 和自定义 ViewController 中。

【问题讨论】:

您设置通知的方式会立即触发。你甚至没有时间关闭你的应用程序。如果您的应用甚至不在后台,您如何期望收到通知? useyourloaf.com/blog/local-notifications-with-ios-10 参考这一切步骤它工作正常。 import UserNotifications import this 并声明 this 代表 UNUserNotificationCenterDelegate 并将其添加到 didFinishLaunchingWithOptions 方法中 let center = UNUserNotificationCenter.current() center.delegate = self return true 还要检查您是否有权接收上升通知。别忘了检查这个。 【参考方案1】:

当您在该检查中创建通知时

 if #available(iOS 10.0, *) 
     let content = UNMutableNotificationContent()
     content.title = "Intro to Notifications"
     content.subtitle = "Lets code,Talk is cheap"
     content.body = "Sample code from WWDC"
     content.sound = UNNotificationSound.default()
            // Deliver the notification in five seconds.
     let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
     let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

     UNUserNotificationCenter.current().delegate = self
     UNUserNotificationCenter.current().add(request)(error) in

           if (error != nil)

                    print(error?.localizedDescription)
           
       
 

实现此委托方法 UNUserNotification 以触发通知。

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 

    print("Tapped in notification")



//This is key callback to present notification while the app is in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 

    print("Notification being triggered")
    //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
    //to distinguish between notifications
    if notification.request.identifier == requestIdentifier

        completionHandler( [.alert,.sound,.badge])

    

编码愉快。

【讨论】:

以上是关于本地通知 swift 3的主要内容,如果未能解决你的问题,请参考以下文章

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

使用本地通知 api 在 swift 3 中触发每周通知

使用开关启用和禁用本地通知(swift 3)

Swift 3 本地通知未触发

Swift 3 - 本地通知的 didReceiveRemoteNotification 函数未触发

本地通知 - 在 swift 3 中重复间隔