gpt4 book ai didi

ios - Firebase 推送通知仅显示在控制台中,不会在后台加载

转载 作者:行者123 更新时间:2023-11-30 12:48:38 26 4
gpt4 key购买 nike

我准备了所有证书,将它们上传到 Firebase 控制台,更新了我的应用 ID 和 Xcode 项目中的设置。但是我的应用程序无法显示任何推送通知。如果我从 Firebase 控制台发送推送通知,我只会在运行应用程序而不是在后台模式下时在控制台中看到该消息。我使用真实设备/iPhone 6S。

此外,如果我注释 AppDelegate 中的所有断开连接部分,控制台会显示以下错误。

Unable to connect with FCM. Optional(Error Domain=com.google.fcm Code=2001 "(null)") 

我在 AppDelegate.swift 中准备了 Firebase 推送通知:

import UIKit
import UserNotifications

import Firebase
import FirebaseAuth
import FirebaseMessaging
import FirebaseDatabase
import FirebaseInstanceID
import FirebaseRemoteConfig

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
application.statusBarStyle = .lightContent
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true

//let _ = RemoteConfigValues.sharedInstance

let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
logUser()

return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""

for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}

FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)

print("tokenString: \(tokenString)")
}

// [START receive_message]
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (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

// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"])")

// Print full message.
print("%@", userInfo)
}
// [END receive_message]

// [START refresh_token]
func tokenRefreshNotification(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")

// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
// [END refresh_token]

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print(error.localizedDescription)
}

// [START connect_to_fcm]
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
// [END connect_to_fcm]

func applicationDidBecomeActive(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0
connectToFcm()
}

// [START disconnect_from_fcm]
func applicationDidEnterBackground(_ application: UIApplication) {
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
// [END disconnect_from_fcm]


}

最佳答案

即使我遇到了这个错误,当我更改发送给以下人员的通知数据时,它对我有用

{"to":"coYbqscbjkA,S","优先级":"高" "notification":{"body":"12345 ","service_type":"我的服务"}}

检查一次...希望它有效...!

关于ios - Firebase 推送通知仅显示在控制台中,不会在后台加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41300769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com