私は通知をスケジュールし、アプリケーションが実行中であるか、フォアグラウンドで実行されていないときにユーザーに表示することができました。この通知のタップに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
で動作するので、AppDelegate
でUNUserNotificationCenterDelegate
を実装し、didFinishLaunchingWithOptions
方法でデリゲートを設定する必要がUserNotifications
枠組みについては
ハワイNiravは過去数日間質問してきたすべてのiOSに関する質問に回答してくれてありがとう。非常に新しいので、私は質問がいっぱいです;)そしてはいあなたの答えは動作しますが、通知が受信されると解雇されます。 ** 'タップ'イベントをキャプチャするためにどのような機能を実装する必要がありますか?** – Dinuka
@RickGrimesLikesWalkerSoupこのメソッドを実装するには、https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter of 'UNUserNotificationCenterDelegate' 。 –
美しい。できます。あまり問題がなければ最後のサブ質問をしてもらえますか?どの通知をタップしたのかを特定する方法はありますか?のように、その識別子で通知を識別し、別のView Controllerを開きますか? – Dinuka