0

私は通知をスケジュールし、アプリケーションが実行中であるか、フォアグラウンドで実行されていないときにユーザーに表示することができました。この通知のタップにViewControllerと表示する必要があります。Swift 3 - ローカル通知のdidReceiveRemoteNotification機能が起動されない

didReceiveRemoteNotificationは、ユーザーが通知をタップしたときに呼び出される関数だと思います。私の場合、この機能は決して発動されません。

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

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

     return true 
} 

//didReceiveRemoteNotification goes here 

didReceiveRemoteNotification機能:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 
     if (application.applicationState == UIApplicationState.active) 
     { 
      print("Active") 
      // App is foreground and notification is recieved, 
      // Show a alert. 
     } 
     else if(application.applicationState == UIApplicationState.background) 
     { 
      print("Background") 
      // App is in background and notification is received, 
      // You can fetch required data here don't do anything with UI. 
     } 
     else if(application.applicationState == UIApplicationState.inactive) 
     { 
      print("Inactive") 
      // App came in foreground by used clicking on notification, 
      // Use userinfo for redirecting to specific view controller. 
     } 
} 

これは私のAppDelegate全体の通知関連のコードです。何か不足していますか?あなたはUNUserNotificationCenterDelegateで動作するので、AppDelegateUNUserNotificationCenterDelegateを実装し、didFinishLaunchingWithOptions方法でデリゲートを設定する必要がUserNotifications枠組みについては

答えて

2

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

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

    return true 
} 

今、あなたは、通知を得るためにuserNotificationCenter(_:willPresent:withCompletionHandler:)userNotificationCenter(_:didReceive:withCompletionHandler:)のメソッドを実装する必要があります。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

    //Handle notification 
} 

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    // pull out the buried userInfo dictionary 
    let userInfo = response.notification.request.content.userInfo 

    if let customData = userInfo["customData"] as? String { 
     print("Custom data received: \(customData)") 

     switch response.actionIdentifier { 
     case UNNotificationDefaultActionIdentifier: 
      // the user swiped to unlock 
      print("Default identifier") 

     case "show": 
      // the user tapped our "show more info…" button 
      print("Show more information…") 
      break 

     default: 
      break 
     } 
    } 

    // you must call the completion handler when you're done 
    completionHandler() 
} 

また、詳細については、このAppCodaチュートリアルIntroduction to User Notifications Framework in iOS 10を確認することができます。

+0

ハワイNiravは過去数日間質問してきたすべてのiOSに関する質問に回答してくれてありがとう。非常に新しいので、私は質問がいっぱいです;)そしてはいあなたの答えは動作しますが、通知が受信されると解雇されます。 ** 'タップ'イベントをキャプチャするためにどのような機能を実装する必要がありますか?** – Dinuka

+0

@RickGrimesLikesWalkerSoupこのメソッドを実装するには、https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter of 'UNUserNotificationCenterDelegate' 。 –

+0

美しい。できます。あまり問題がなければ最後のサブ質問をしてもらえますか?どの通知をタップしたのかを特定する方法はありますか?のように、その識別子で通知を識別し、別のView Controllerを開きますか? – Dinuka

関連する問題