问题描述
为了尝试使用Firebase推送通知,我遵循以下示例: https://github.com/firebase/quickstart-ios/blob/dc2cd2db6e82e5c475fa3f0efe75df8b54f04544/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
In order to try out Push Notifications with Firebase I am following this example:https://github.com/firebase/quickstart-ios/blob/dc2cd2db6e82e5c475fa3f0efe75df8b54f04544/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
我可以使其发挥到一定程度.
I could make it work up to a certain point.
这是我拥有的相关代码:
Here is the relevant code I have:
......
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
......
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
FirebaseApp.configure()
......
return true
}
......
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
print(#function)
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler()
}
......
当我尝试对其进行测试时,函数 userNotificationCenter:didReceive:withCompletionHandler()是唯一被调用的函数(在示例提供的函数列表中).还应该注意的是,我在调试器控制台上看到了此消息,但设备上没有任何反应.我想这就是应该的样子(所以一切都很好),但这是我对任何可以回答的人都有的问题.
When I try to test it, the function userNotificationCenter:didReceive:withCompletionHandler() is the only one called (in the list of function provided by the example). It should also be noticed that I can see this looking at the debugger console and nothing is happening on the device. I guess that's the way it's supposed to be (so all is fine), but here is a question I have for anyone who can answer.
如何使用 completionHandler()?我想,因为它是一个参数,所以我可以以一种相当自由的方式来使用它,但是当我在网上寻找示例代码时,我只能看到它在参数列表中声明或像示例中那样我正在与之合作,但从未见过定义.
How do I make use of completionHandler() ?I suppose, since it's a parameter I can make some use of it in a rather free way, but when I look for sample code on the net I can only see it declared in the list of parameters or called as is the case in the example I am working with, but I never see it defined.
我应该看看一些东西还是如何使用它的具体例子吗?
Is there something I should look at or a concrete example of how to make use of it?
有关信息,我正在使用Xcode版本10.1,iOS 12.1和Swift 4.2.
For information, I am using Xcode Version 10.1, iOS 12.1 and Swift 4.2.
推荐答案
如Apple的文档,此方法中的completionHandler
是:
As explained in Apple's documentation, completionHandler
in this method is:
因此,处理完通知后,应调用完成处理程序.您不能以任何其他方式使用它.调用后,系统便知道您已经完成了通知,并可以执行任何所需的过程,例如将通知放在用户的通知中心.
So you should call completion handler when you are done processing the notification. You cannot use it in any other way. Once you call it, the system then knows you are done with the notification and can perform any needed processes, such as putting the notification in the user's notification center.
这篇关于试用Firebase-推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!