如何使用FireBase更新推送通知

时间:2017-06-24 10:24:50

标签: ios swift xcode firebase

我目前正在尝试设置推送通知,尽管xcode认为代码中没有任何问题,如果任何人都可以看到任何问题,或者对其他地方有任何问题可能会对此有所了解,那将非常感谢。

 //
//  AppDelegate.swift
//  PushMyNotifs
//
//  Created by Jack Wallace on 24/6/17.
//  Copyright © 2017 JackWallace. All rights reserved.
//

import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, 
UNUserNotificationCenterDelegate {

var window: UIWindow?


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


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

    application.registerForRemoteNotifications()


    FirebaseApp.configure()



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

    return true

}

func applicationDidEnterBackground(_ application: UIApplication) {

    Messaging.messaging().shouldEstablishDirectChannel = false

}

func tokenRefreshNotification(notification: NSNotification){

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

    print("InstanceID token: \(refreshedToken)")

    connectToFCM()

}



func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {

    print("Firebase registration token: \(fcmToken)")

}



func connectToFCM(){

    Messaging.messaging().shouldEstablishDirectChannel = false

    }

 }

1 个答案:

答案 0 :(得分:0)

您是否设置了证书?

这是一个很好的例子: push-notifications-in-firebase