Swift 3-本地通知';s DIDreceiveemotentification函数未激发
我已成功安排了一个通知,并在应用程序在前台运行/未运行时向用户显示该通知。现在我需要在点击此通知时显示一个Swift 3-本地通知';s DIDreceiveemotentification函数未激发,swift,notifications,swift3,uilocalnotification,usernotifications,Swift,Notifications,Swift3,Uilocalnotification,Usernotifications,我已成功安排了一个通知,并在应用程序在前台运行/未运行时向用户显示该通知。现在我需要在点击此通知时显示一个ViewController func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentati
ViewController
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()
}
我知道,didReceiveMemotentification
是用户点击通知时调用的函数。在我的例子中,这个函数永远不会被激发。
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
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.
}
}
DidReceiveMotonification功能:
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
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.
}
}
这是myAppDelegate
中与通知相关的全部代码。我遗漏了什么吗?对于您需要使用的框架,请使用AppDelegate
实现UnuseNotificationCenterDelegate
,并在didFinishLaunchingWithOptions
方法中设置委托
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
}
现在您需要实现和方法来获取通知
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教程以了解更多详细信息。哈哈,感谢您为我在过去几天提出的所有iOS问题提供答案。非常新,所以我有很多问题;)是的,你的答案是有效的,但是当收到通知时就会被触发。我需要实现什么函数来捕获“tap”事件?@RickGrimesLikesWalkerSoup为此实现了
UnuseNotificationCenterDelegate
的此方法。它起作用了。如果不太麻烦的话,我可以问最后一个小问题吗。我如何识别被点击的通知?如中所示,通过其标识符识别通知,然后打开不同的视图控制器?@RickGrimesLikesWalkerSoup您可以使用response.actionIdentifier
获得该通知。您可以将我链接到一个示例吗?