gpt4 book ai didi

ios - PushNotifications 在 iOS 中无法正常(可靠)工作

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

我在推送通知方面遇到一些问题。我们使用 Firebase 来推送通知。问题是我可以在一台 iPhone 上正确接收 PushNotifications,但无法在另一台 iPhone 上接收它们。我在 Android 上正确收到通知。

我正在做的是从同一帐户登录并尝试推送通知。发生的情况是,我在所有 Android 手机上都遇到了这种情况,其中大部分是在我的 iPhone 5S 上,但在我的 iPhone 6 上却很少。

以下是 AppDelegate 中用于设置连接的代码。

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
InstanceID.instanceID().setAPNSToken(deviceToken, type: InstanceIDAPNSTokenType.sandbox)
}

@objc func tokenRefreshNotification(notification: Notification){
print("entered tokenRefreshNotification")
let refreshedToken = InstanceID.instanceID().token()
if refreshedToken != nil{
UserDefaults.standard.setValue(refreshedToken, forKey: "token")
}
//UserDefaults.standard.setValue(refreshedToken, forKey: "token")
print("Instance ID token: \(String(describing: refreshedToken))")
connectToFCM()
}


func connectToFCM(){
Messaging.messaging().connect { (error) in
if error != nil{
self.print("Unable to connect to FCM \(String(describing: error))")
return
}
self.print("connected to FCM")
}
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("application did receive remote notification")
print(userInfo)
let notification = UILocalNotification()
notification.fireDate = Date()
let notificationType = (userInfo["type"] as! String).lowercased()

switch notificationType {
case "alert" :
print("Show notifications and save in the notifications list.")
notification.alertBody = userInfo["message"] as? String
//code for saving to user defaults
if var notifications = userDefaults.value(forKey: keys.notificationsList) as? [String] {
if let notificationString = notification.alertBody{
notifications.append(notificationString)
userDefaults.setValue(notifications, forKey: keys.notificationsList)
}
} else{
//in case of empty notificationList
if let notificationString = notification.alertBody{
let notifications = [notificationString]
userDefaults.setValue(notifications, forKey: keys.notificationsList)
}
}

//notifications.append(notification.alertBody)
UIApplication.shared.scheduleLocalNotification(notification)
case "advertisement" :
print("Just show the notification and do nothing else.")
notification.alertBody = userInfo["message"] as? String
UIApplication.shared.scheduleLocalNotification(notification)
case "cleardb":
print("Clear everything from Database, but not logout user.")
notification.alertBody = "All data from database had been wiped"
UIApplication.shared.scheduleLocalNotification(notification)
case "update_device":
print("device_data has been updated so download devices info again")
case "logout":
print("logout the user")
Functions.functions.wipeUserDefaults()

notification.alertBody = "Logged out of Ryoking"
UIApplication.shared.scheduleLocalNotification(notification)
default:
print("lol")
}
}


func application(received remoteMessage: MessagingRemoteMessage) {
print("application received remote message")
print(remoteMessage.appData)
}

最佳答案

我已经从 firebase 控制台字面上执行了以下步骤,它对我来说就像一个魅力,尝试一下:

https://firebase.google.com/docs/cloud-messaging/ios/first-message?authuser=0

关于ios - PushNotifications 在 iOS 中无法正常(可靠)工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942666/

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