2017-07-01 7 views
1

私はアプリは、アクションレスポンスを受信するたびにAppDelegate.swiftに自分のアプリケーションのデータにアクセスできるようにするにはローカル通知の本文テキストや識別子を取得します。私は使用しようとしていたAppDelegateスウィフト

方法、私は通知の識別子や本文を取得できないため、データを見つけることができません。誰かが私を助けてくれましたか?みんなありがとう。

もっとコード:

//Setting content of the notification 
let content = UNMutableNotificationContent() 
content.title = "Scheduled Task" 
content.body = taskDescriptionTextField.text! 
content.badge = 1 
content.sound = UNNotificationSound.default() 
content.categoryIdentifier = "alertCategory" 
//Setting time for notification trigger 
let date = datePicker.date 
let dateCompenents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date) 
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false) 
//Adding Request 
let request = UNNotificationRequest(identifier: taskDescriptionTextField.text!, content: content, trigger: trigger) 
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
+0

のいずれかを実行している可能性があります。いくつかのコードを追加してください。 –

+0

thxを助けてください。私はメソッドのdidReceiveの応答を呼び出すことによって、通知の身体の内容や識別子を取得する方法を知らない...他のViewControllerからそれらのデータをAppDelegateに送ることができる方法はありますか?下のViewControllerのコードをもっと見る –

+0

はこのtutを見るかもしれません: - http://www.appcoda.com/local-notifications-ios8/ それはすべての基本的なものをカバーします。 –

答えて

3

の操作を行います。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    print("original identifier was : \(response.notification.request.identifier)") 
    print("original body was : \(response.notification.request.content.body)") 
    print("Tapped in notification") 
} 

基本的にそれが何であるか、あなたが戻ってUNNotificationResponseインスタンスを取得していることです。そのオブジェクトには2つのプロパティがあります。

  • var actionIdentifier: String
  • var notification: UNNotification < - あなたはこの1つを使用する必要があります。またUNUserNotificationフレームワークのために非常に非常に良いチュートリアルでは、それらが通知(提示されたときにユーザーが選んだのどのアクションを調べたい場合にのみactionIdenfierを使用here


    見つけることができます

//アップルのドキュメントから:ユーザが選択できるアクション識別子:

0からの選択肢はありませんか?いいえ、あなたはそれを拒否しましたか?それが何を意味
* UNNotificationDismissActionIdentifier if the user dismissed the notification 
* UNNotificationDefaultActionIdentifier if the user opened the application from the notification 
* the identifier for a registered UNNotificationAction for other actions 

で、あなたのような何かをするために使用することができます:あなたが持つべきカスタムアクションを作成するには

switch actionIdentifier { 
case UNNotificationDismissActionIdentifier: // Notification was dismissed by user 
    // Do something 
    completionHandler() 
case UNNotificationDefaultActionIdentifier: // App was opened from notification 
    // Do something 
    completionHandler() 
    // Do something else 
customAction: 
    completionHandler() 
default: 
    completionHandler() 
} 

を:

  • は、カテゴリ
  • 内部のアクションを作成します。
  • カテゴリを登録

詳細については、WWDC 2016 Advance Notifications

関連する問題