我最近在我的应用程序上设置了通知,我可以通过php发送,不用担心,一切正常。
自上周以来,我试图找出手机是否收到推送通知..
我可以知道有人点击它但是如果该人没有点击它并且更喜欢打开该应用程序,则它无法正常工作..
是否有一个简单的函数可以告诉我应用程序刚刚收到通知?
在我的AppDelegate.swift中:
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Check if launched from notification
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
//print(notification)
window?.rootViewController?.present(ViewController(), animated: true, completion: nil)
}
else{
//print("ici ?")
registerForRemoteNotification()
}
return true
}
...
//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
...
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
**print("here ?")**
switch((application.applicationState)){
case UIApplicationState.inactive:
print("Inactive")
//Show the view with the content of the push
completionHandler(.newData)
case UIApplicationState.background:
print("Background")
//Refresh the local model
completionHandler(.newData)
default:
print("Active")
//Show an in-app banner
completionHandler(.newData)
break
}
}
}
问题:我从未传入 didReceiveRemoteNotification :/ "打印在这里"永远不会显示。
我在这一行上有一个错误:
实例方法 '应用程序(应用程序:didReceiveRemoteNotification:fetchCompletionHandler:)' 几乎匹配可选要求 '应用程序(_:didReceiveRemoteNotification:fetchCompletionHandler:)' 协议' UIApplicationDelegate'
但我不明白:/
你知道吗?谢谢你的帮助^^
答案 0 :(得分:2)
自转换为Swift 3以来,这是一个相当常见的错误 - 您正在使用Swift 2方法。
Swift 3中此方法的正确签名是:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("here?")
}