【发布时间】: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