FCM不向所有用户发送消息

时间:2018-03-29 02:54:14

标签: ios firebase swift3 firebase-cloud-messaging

我只在我的测试设备上收到通知,但是在用户的设备上没有收到该消息。可能是什么错误?

import UIKit
import Firebase
import IQKeyboardManagerSwift
import SVProgressHUD
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.



        window?.rootViewController = initialVC
        window?.makeKeyAndVisible()


        //Config. Firebase
        FirebaseApp.configure()


        //Config. Push Notification
        if #available(iOS 10.0, *) {

            UNUserNotificationCenter.current().delegate = self
            Messaging.messaging().delegate = self

            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in

                if error == nil{
                    print("Autorizacion exitosa")
                }

            }
        } else {
            // Fallback on earlier versions
            let setting : UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(setting)
        }


        application.registerForRemoteNotifications()

        NotificationCenter.default.addObserver(self, selector: #selector(self.refreshToken(notification:)), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)


        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

        Messaging.messaging().shouldEstablishDirectChannel = false
        application.applicationIconBadgeNumber = 0

    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.



    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        ConnectToFCM()

    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


    @objc func refreshToken(notification: NSNotification){

        let refreshToken = InstanceID.instanceID().token()!

        print("refreshToken: \(refreshToken)")

        ConnectToFCM()

    }


    func ConnectToFCM(){

        Messaging.messaging().shouldEstablishDirectChannel = true

    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }




}

0 个答案:

没有答案