【问题标题】:Notification iOs Push delivered?通知 iOs 推送已交付?
【发布时间】:2016-12-07 12:41:59
【问题描述】:

我最近在我的应用程序上设置了通知,我可以通过 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'

但我不明白:/

你有什么想法吗?

感谢您的帮助^^

【问题讨论】:

  • 无论您在第三次解释中说什么,只有我们可以检查用户点击通知。当用户滑动通知无法检查时,此功能在苹果文档中不可用。
  • 无法检查我的应用是否收到推送? O_o
  • stackoverflow.com/questions/25830597/… ,带链接,我研究了也不能保证说交付与否。在我们的应用程序中,我们也尝试了这个,那时我才知道不可能,只有当用户点击那个时才。
  • 我阅读了链接,但我有相反的例子:WhatsApp 如何检测是否在未在 iOS 上打开应用程序的情况下发送了消息?应该可以的:p

标签: ios swift push-notification notifications swift3


【解决方案1】:

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

这个方法在 Swift 3 中的正确签名是:

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

【讨论】:

  • 哦,这只是一个糟糕的复制/粘贴......在我的代码中,它是正确的签名,对于 Swift 3.. 我的坏^^但它仍然不起作用:/
  • 你在 iOS 10 上运行它吗?
  • 你是否收到了这个应用程序的推送?有声音等。
  • 是的,推送工作正常,但我不知道如何“抓住”当它通过电话传递时:/
  • 你需要“抓”什么?如果用户点击推送,应用程序应该在 didFinishLaunchingWithOptions(如果应用程序被终止)或 didReceiveRemoteNotification(如果应用程序在后台)中知道这一点
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 2011-08-07
  • 1970-01-01
相关资源
最近更新 更多