我设法安排了一个通知,并在应用运行/未在前台运行时将其显示给用户。现在我需要在点击此通知后显示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
整个通知相关的代码。我错过了什么吗?
Haha Nirav感谢您在过去的几天中询问过的所有iOS问题的答案。非常新,所以我充满了问题;)是的,你的答案有效,但当收到通知时它会被解雇。 **我需要实现什么功能来捕获'tap'事件?** – Dinuka
@RickGrimesLikesWalkerSoup对于'UNUserNotificationCenterDelegate'实现此方法https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter 。 –
美丽。有用。请问最后一个问题是不是太麻烦。我怎样才能确定哪个通知被点击了?如在,通过它们的标识符来识别通知,然后打开不同的视图控制器? – Dinuka