繁体   English   中英

通知iOs推送已交付?

[英]Notification iOs Push delivered?

我最近在我的应用程序上设置了通知,我可以通过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 :/永远不会显示“此处打印”。

我在这条线上有一个错误:

实例方法“ application(application:didReceiveRemoteNotification:fetchCompletionHandler :)”几乎与协议“ UIApplicationDelegate”的可选要求“ application(_:didReceiveRemoteNotification:fetchCompletionHandler :)”匹配

但是我不理解 :/

你有想法吗 ?

谢谢你的帮助^^

自从转换为Swift 3以来,这是一个比较常见的错误-您使用的是Swift 2方法。

Swift 3中此方法的正确签名是:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("here?")
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM