swift - Swift didReceiveRemoteNotification() Firebase 未被调用
问题描述
在广泛阅读后,我找不到解决方案。我正在从 Firebase 向我的 ios 应用程序发送云消息,但是根本没有调用方法 didReceiveRemoteNotification(),而不是当应用程序处于前台而不是后台时。我尝试从firebase控制台发送,也从邮递员使用主题发送,但两者都不起作用。我已经在应用程序中集成了 Firebase,并将证书上传到 Firebase,我在控制台中没有收到任何错误。只是通知不来?我还添加了云消息的功能。
这就是我在 appDelegate 中所做的(来自 Firebase 教程):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
解决方案
推荐阅读
- jquery - 如何一起使用 bootstap4 工具按钮和 jquery 验证插件
- python - 如何修复此错误:python 代码中的 ValueError 以及来自 tensorflow (GPflow) 的示例
- node.js - 为什么 Dialogflow 导入会在导入时出错?
- r - 难以让嵌套 Shiny 模块中的 actionButton 正常工作
- scala - 如何将多个 Spark ml 模型拟合到单个 Dataset/DataFrame 的每个分区子集?
- java - 使用 JRImageLoader.loadAwtImageFromBytes 方法加载图像时出错
- mutt - Mutt-无法连接到 smtp.gmail.com(网络无法访问)
- bash - 无法在 case 语句中执行 ifelse 语句
- regex - htaccess 正则表达式不附加获取值
- ansible - 在 Ansible 的 API 调用中使用子元素