在Swift中对Firebase远程通知采取行动

时间:2019-03-07 14:00:34

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

我正在尝试处理从Firebase控制台发送的通知,当用户点击通知时,它会执行特定操作,但是 例如:如果密钥是主页,则将用户带到应用程序的主页。 那就是我的应用程序代表,现在要做的就是打开应用程序。在控制器视图中是否应该做任何事情

var window: UIWindow?
let  gcmMessageIDKey = "gcm.message_id"

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    Messaging.messaging().appDidReceiveMessage(userInfo)

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")

    }

    // Print full message.
    print(userInfo)

    completionHandler(UIBackgroundFetchResult.newData)
}
func messaging(_ messaging: Messaging, didReceiveremoteMessage: MessagingRemoteMessage) {
    //         Convert to pretty-print JSON, just to show the message for testing

}

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

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

    FirebaseApp.configure()
    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 })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    Messaging.messaging().delegate = self

    application.registerForRemoteNotifications()


    return true
}


func applicationWillResignActive(_ application: UIApplication) {

}
func applicationDidEnterBackground(_ application: UIApplication) {
}

func applicationWillEnterForeground(_ application: UIApplication) {
    connectToFCM()
}
func applicationDidBecomeActive(_ application: UIApplication) {
}

func applicationWillTerminate(_ application: UIApplication) {
}
func tokenRefreshNorification(notification : NSNotification){
    let refreshedToken = InstanceID.instanceID().token()
    print("InstanceID Token: \(String(describing: refreshedToken))")
    FirebaseApp.configure()
    connectToFCM()
}
func connectToFCM(){
    Messaging.messaging().connect { (error) in
        if (error != nil){
            print("unable to connect to FCM \(error)")
        }else{
            print("Connected To FCM")
        }
    }
}

}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    Messaging.messaging().appDidReceiveMessage(userInfo)


    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print("user info is :" ,userInfo)

    // Change this to your preferred presentation option
    completionHandler([.alert, .sound, .sound])
}

0 个答案:

没有答案