【发布时间】:2017-07-25 17:41:37
【问题描述】:
我正在尝试使用 FCM 令牌从 firebase 通知控制台向特定设备发送一个简单的推送通知。 Firebase 通知控制台将通知显示为已发送,但设备未收到通知。我已尝试发送通知,然后等待查看控制台是否从didReceiveRemoteNotification 记录日志,但通知需要很长时间(数小时)才能显示为在 firebase 控制台中发送(即使我将优先级设置为high) .
应用代理
import UIKit
import Firebase
import FirebaseStorage
import FirebaseDatabase
import FirebaseMessaging
import CoreData
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Use Firebase library to configure APIs
FirebaseApp.configure()
/////
// For Firebase Cloud Messaging (FCM)
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()
// End of [for Firebase Cloud Messaging (FCM)]
/////
return true
}
///////////////////////
// FCM Setup
// Monitor token generation for FCM: Be notified whenever the FCM token is updated
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
// Monitor token generation for FCM:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
} // Handle messages received through the FCM APNs interface
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print("didReceiveRemoteNotification")
// 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.
// gcm_message_id
if let messageID = userInfo["gcmMessageIDKey"] {
print("Message ID: \(messageID)")
}
^我的猜测是问题可能与“gcm_message_id”/“gcmMessageId”/“gcm.message_id”有关,因为我在下面尝试的三种方法中的每一种都不同
// Print full message.
print(userInfo)
}
// Handle messages received through the FCM APNs interface
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("didReceiveRemoteNotification (withCompletionHandeler)")
// 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)")
}
^我的猜测是问题可能与“gcm_message_id”/“gcmMessageId”/“gcm.message_id”有关,因为我在下面尝试的三种方法中的每一种都不同
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
// End of [FCM Setup]
///////////////////////
}
视图控制器
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Retrieve the current registration token for Firebase Cloud Messaging (FCM)
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
}
}
权利和启用推送通知
我添加了推送权利 并启用了推送通知 的后台模式,并将GoogleService-Info.plist 添加到我的项目中。
通知发送方法
我正在从 Firebase 通知控制台(如下所示)创建通知,因此通知本身的结构应该没有问题。
我尝试了以下方法来解决这个问题,但都产生了相同的结果:
有谁知道为什么通知在 firebase 通知控制台中被标记为已发送但未显示在设备上?
【问题讨论】:
-
请检查以下内容: 1. 您是否添加了推送权限? 2. 是否添加了 GoogleService-Info.plist?
-
@jn-se,感谢您的评论。我添加了推送权利(并为推送通知启用了后台模式)并将 GoogleService-Info.plist 添加到我的项目中。我已将屏幕截图添加到我上面的问题中
-
除了权利:1。 “但是通知需要太长时间(小时)才能显示为在 Firebase 控制台中发送(即使我将优先级设置为高)。`” 结构 及其外观。很多时候payload的结构是错误的,因此你什么也得不到!
-
1.我已经研究过如何解决这个问题,但只发现将优先级设置为
high,就像我现在所做的那样;如果您对修复有任何建议,我们将不胜感激。 2. 我已经确定我没有收到错误,并且正在将 FCM 令牌打印到我在 xcode 中的控制台上,并使用它来定位适当的设备。 3. 我自己还没有创建结构(以消除您描述的风险),而是从 Firebase 通知控制台创建通知;我在我的问题中添加了一张图片,以便您查看
标签: ios swift firebase firebase-cloud-messaging firebase-notifications