Firebase推送通知不在后台运行

时间:2018-04-18 12:19:02

标签: ios swift firebase firebase-cloud-messaging

我正在使用firebase云消息发送推送通知。收到推送通知后,我下载了一些其他内容,然后发出本地通知。这适用于前台但不适用于后台。

我在youtube上关注了此tutorial。 这是我的代码,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted, error) in
        if granted {
            UNUserNotificationCenter.current().delegate = self
            Messaging.messaging().delegate = self
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
            FirebaseApp.configure()
        }
    }
}

func applicationDidEnterBackground(_ application: UIApplication) {
    Messaging.messaging().shouldEstablishDirectChannel = false
}

func applicationDidBecomeActive(_ application: UIApplication) {
    connectFCM()
}

func connectFCM() {
    Messaging.messaging().shouldEstablishDirectChannel = true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    UIApplication.shared.beginBackgroundTask {
    }
    fireLocalNotification(data:userInfo)// where additional content is downloaded and a local notification is fired
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    token = fcmToken
    sendTokenToServer(token: fcmToken)
    connectFCM()
}

我已在info.plist文件中添加了这些后台模式 enter image description here

0 个答案:

没有答案
相关问题