我正在尝试将firebase消息传递到我的应用程序中,但我只是在应用程序处于活动状态时收到通知(我可以在控制台中看到它们,但仅在应用程序处于活动状态时才会看到。如果它已关闭它们在应用再次激活之前不会出现。)
我已启用后台通知,以及我的p12证书已加载到firebase设置中。
以下是我的app delegate中的代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Fabric.with([Crashlytics.self])
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
if #available(iOS 11.0, *){
print("IOS 11")
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
}else 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()
return true
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
@nonobjc func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
// Convert to pretty-print JSON
guard let data =
try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted),
let prettyPrinted = String(data: data, encoding: .utf8) else {
return
}
print("Received direct channel message:\n\(prettyPrinted)")
}
func application(received remoteMessage: MessagingRemoteMessage){
print("Received Remote Message")
guard let data =
try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted),
let prettyPrinted = String(data: data, encoding: .utf8) else {
return
}
print("Received direct channel message:\n\(prettyPrinted)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print(userInfo)
}
编辑1: 这是我在控制台中收到的示例通知
{
"from" : "480981200252",
"notification" : {
"e" : "1",
"body" : "hey"
},
"collapse_key" : "com.amplesftwr.thelifeofcarl"
}
答案 0 :(得分:2)
在“项目设置”中的“功能”下,确保不仅打开“推送通知”,还打开“背景模式”。
在.plist文件中,您还应该拥有一个“必需的后台模式”键,并在项目中确保您也有.entitlements文件。
您可能想要检查的另一件事是,当您转到Firebase控制台时,不要仅将通知发送给此特定用户的令牌,请尝试使用此选项将该消息发送给该应用的所有用户Firebase控制台。
如果有效,那么您的问题可能与FCM令牌有关。
如果它仍然不起作用,那么您可能需要检查您的钥匙链和Apple开发人员帐户中的所有证书,以确保一切都是绿色/好的,并且没有重复!
我无法评论你的帖子,所以我在这里作为答案添加它,希望它能帮助你解决你的问题,但如果没有,那么,请让我知道,我会尽我所能:)