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

时间:2017-12-22 13:52:17

标签: ios swift apple-push-notifications

我正面临推送通知的一些问题。我们正在使用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)
}

1 个答案:

答案 0 :(得分:0)

我从firebase控制台字面上按照这个步骤操作,它对我来说就像一个魅力,尝试一下:

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