本地通知swift 3

问题描述:

我试图通过点击按钮发送通知。这是我的viewController代码:我已经要求使用通知权限本地通知swift 3

@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() 

附:我已经在AppDelegate和自定义ViewController中导入了

import UserNotifications 

+0

您设置通知的方式会立即启动。你甚至没有时间关闭你的应用程序。如果您的应用程序甚至不在后台,您希望如何收到通知? –

+0

https://useyourloaf.com/blog/local-notifications-with-ios-10/参考这一切步骤其工作正常。 –

+0

进口UserNotifications导入,并宣布该该代表UNUserNotificationCenterDelegate和 在didFinishLaunchingWithOptions方法中添加此 让中心= UNUserNotificationCenter.current() center.delegate =自 还真 –

当你在那个检查

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]) 

    } 
} 

快乐编码。