本地通知 Swift 3

3

我正在尝试在按钮点击时发送通知。这是我的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中都要执行。


你设置的通知会立即触发,甚至连关闭应用程序的时间都没有。如果你的应用程序甚至不在后台运行,你怎么能接收到通知呢? - Leo Dabus
请参考以下链接中的所有步骤,它们可以在iOS 10上正常工作:https://useyourloaf.com/blog/local-notifications-with-ios-10/ - Himanshu Moradiya
导入UserNotifications,声明UNUserNotificationCenterDelegate代理并将其添加到didFinishLaunchingWithOptions方法中:let center = UNUserNotificationCenter.current() center.delegate = self return true - Himanshu Moradiya
请确保您已经被授权进行提醒通知。不要忘记检查这一点。 - Himanshu Moradiya
1个回答

2
当您创建通知时,请检查以下内容:
 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])

    }
}

愉快的编码。


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