2017-06-24 31 views
0

我目前正在嘗試設置推送通知和allthough xcode認爲沒有問題在代碼中feauture不工作,如果任何人都可以看到任何問題或有其他地方的建議一個問題可能會不勝感激。如何使用推送通知與FireBase更新

// 
// 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 

    } 

} 

回答