2017-04-20 10 views
3

ボタンクリックで通知を送信しようとしています。ローカル通知swift 3

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

P.S.:私は通知を使用する許可を要求してきたAppDelegateでも

@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") 
     } 
    } 
} 

:これは私のViewControllerのコードです私は、AppDelegateとカスタムViewControllerの両方で、

import UserNotifications 

をインポートしました。

+0

あなたの通知を設定した方法は、すぐに消えます。あなたはあなたのアプリを閉じる時間がありません。あなたのアプリがバックグラウンドでさえもない場合に、どうやって通知を受け取ることを期待していますか? –

+0

https://useyourloaf.com/blog/local-notifications-with-ios-10/こちらの手順をすべて参考にしてください。 –

+0

輸入UserNotificationsがこれをインポートし、この代表者UNUserNotificationCenterDelegateと はセンター= UNUserNotificationCenter.currentを聞かせて didFinishLaunchingWithOptionsメソッドでこれを追加する宣言() center.delegate =また、あなたが立ち上がり、通知のために許可されていることを確認 –

答えて

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

    } 
} 

ハッピーコーディング。

関連する問題