swift4:从服务器发送通知时,Firebase通知无法正常工作

时间:2018-05-16 08:02:16

标签: ios swift firebase firebase-cloud-messaging swift4

我正在使用Swift4: 我想创建firebase推送通知,我会按照此视频firebase push notification的步骤进行操作 这是app委托代码:

import UIKit
import IQKeyboardManagerSwift
import FBSDKLoginKit
import TwitterKit
import Firebase
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    print("user tooooken: ", Common.getUserToken())
    FirebaseApp.configure()
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
        if(error == nil) {
            print("successful authentication")
        }
    }
    application.registerForRemoteNotifications()

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


    UIApplication.shared.statusBarView?.backgroundColor = UIColor(red:255/255.0, green:51/255.0, blue:255/255.0, alpha: 1.0);
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]

    TWTRTwitter.sharedInstance().start(withConsumerKey:"xxxxxxxxxxx", consumerSecret:"xxxxxxxxxxxxxxxxxx")

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func applicationWillResignActive(_ application: UIApplication) {
    FBSDKAppEvents.activateApp()
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

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

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

///// Twitter authentication //////
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if TWTRTwitter.sharedInstance().application(app, open: url, options: options) {
        return true
    }
    return true
}

@objc func refreshToken(notification: NSNotification) {
    let refreshToken = InstanceID.instanceID().token()!
    print(refreshToken)

    FBHandler()
}

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

}

当我从firebase服务器推送通知时,它成功传送到设备,但是当php服务器发送推送通知时它不起作用,我忘记了任何步骤吗?

1 个答案:

答案 0 :(得分:0)

确保这些:

  1. 您已将设备的firebase FCM令牌发送到您的服务器。

  2. 如果您的服务器向您发送了数据通知,那么它就是数据对象 应该在'通知'键中。

  3. 此外,当用户从设备注销时,您必须删除用户的FCM 来自服务器的令牌。