函数didReceive remoteMessage没有被称为Swift 5

时间:2019-05-15 21:41:14

标签: ios swift firebase firebase-notifications swift5

所以我一直在这里和其他地方阅读有关该主题的其他文章,但我仍在努力。我的应用正在使用"Fireabase FCM"进行设备到设备的通知。我能够发送和接收消息。但是,永远不会调用方法"didReceive remoteMessage"。我想使用此功能来更新应用程序和标签栏项的徽章编号。

我尝试添加其他方法来指示收到了一条消息,但是这些方法均无效。我想我缺少基本的东西。我正在将Swift 5Xcode 10一起使用。

import Foundation
import UIKit
import Firebase
import FirebaseFirestore
import FirebaseMessaging
import UserNotifications

class PushNotificationManager : NSObject, MessagingDelegate, UNUserNotificationCenterDelegate  {

    static let shared = PushNotificationManager( )

    var userID: String = ""
    var instanceIDToken : String = ""
    let defaults = UserDefaults.standard

    override init( ) {
        super.init()
    }

    func setUserId(identifier: String) {
        userID = identifier
    }

    func registerForPushNotifications() {
        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 })
            // For iOS 10 data message (sent via FCM)
            Messaging.messaging().delegate = self
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
        }

        UIApplication.shared.registerForRemoteNotifications()
        UNMutableNotificationContent().sound = UNNotificationSound.default


        Messaging.messaging().shouldEstablishDirectChannel = true

        updateFirestorePushTokenIfNeeded()
    }


    func getNotificationSettings( ) {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            print("Notification settings: \(settings)")
            guard settings.authorizationStatus == .authorized else {return}
            DispatchQueue.main.async{UIApplication.shared.registerForRemoteNotifications()}
        }
    }

    func updateFirestorePushTokenIfNeeded( ) {
      //  let mytoken = Messaging.messaging().fcmToken
        if let token = Messaging.messaging().fcmToken {
            let usersRef = Firestore.firestore().collection("users_table").document(userID)
            usersRef.setData(["fcmToken": token], merge: true)
        }
    }

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print(remoteMessage.appData)
    }

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

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        print("REMOTE NOTIFICATION RECEIVED")
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        updateFirestorePushTokenIfNeeded()
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print(response)
    }


    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])
    }

    func getTokenDirectly( ) {
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                print("Error fetching remote instance ID: \(error)")
            } else if let result = result {
                print("Remoted instance ID token: \(result.token)")
                self.instanceIDToken = result.token
                self.updateFirestorePushTokenIfNeeded()
            }
        }
    }
}

同样,我在正在测试的设备上收到通知,但是

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print(remoteMessage.appData)
}

从不叫。

1 个答案:

答案 0 :(得分:0)

似乎您没有调用此方法:

SELECT [Sheet1].[ID], [Sheet2].[ID_EXT] from [Sheet1], [Sheet2]
A As (SELECT [Sheet1].[ID], ([Sheet1].[Email] + ';' + [Sheet2].[Long Email]) as email from [Sheet1] inner join [Sheet2] 
on [Sheet1].[ID] = FORMAT([Sheet2].[ID_EXT],'00000000000')  
WHERE [Sheet2].[Type] = 3 AND UCase [Sheet1].[Email] <> UCase [Sheet2].[Long Email]) 
B As (SELECT [Sheet1].[ID], ([Sheet1].[Batchcode] + ';' + str([Sheet2].[Code])) as Code from [Sheet1] inner join [Sheet2] on [Sheet1].[ID] = FORMAT([Sheet2].[ID_EXT],'00000000000') WHERE [Sheet2].[Type]= 3 AND [Sheet1].[Batchcode]<>FORMAT([Sheet2].[Code],'0000))
SELECT [A].[ID], [A].[Email], [B].[Batchcode] from [A] Full outer join [A] ON [A].[ID]=[B].[ID_EXT]

因为这是将registerForPushNotifications() 设置为delegate委托的方法的地方 不会被召唤