2017-07-14 249 views
0

通知發送到單divice顯示不正確的令牌格式((使用無法通過的火力地堡

林此令牌:

2017年7月14日15:15:06.247:無法獲取APNS令牌錯誤域名= com.firebase.iid代碼= 1001 「(空)」 註冊成功令牌:90377a6eef538be30b735eacb0480a406feadeee851597e857b620a6a345da9e

代碼:

import UIKit 
import Firebase 
import FirebaseMessaging 
import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate{ 

var window: UIWindow? 


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    //create the notificationCenter 
    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) 
     //FIRMessaging.messaging().remoteMessageDelegate = self 

    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 

    application.registerForRemoteNotifications() 

    FIRApp.configure() 
    FIRInstanceID .instanceID().token() 
    return true 
} 

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    var token = "" 
    for i in 0..<deviceToken.count { 
     token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]]) 
    } 
    print("Registration succeeded! Token: ", token) 
} 

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    print("Registration failed!") 
} 

func applicationWillResignActive(_ application: UIApplication) { 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 
} 

func applicationDidEnterBackground(_ application: UIApplication) { 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

func applicationWillEnterForeground(_ application: UIApplication) { 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
} 

func applicationDidBecomeActive(_ application: UIApplication) { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

func applicationWillTerminate(_ application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

// Firebase notification received 
@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { 

    // custom code to handle push while app is in the foreground 
    print("Handle push from foreground\(notification.request.content.userInfo)") 

    let dict = notification.request.content.userInfo["aps"] as! NSDictionary 
    let d : [String : Any] = dict["alert"] as! [String : Any] 
    let body : String = d["body"] as! String 
    let title : String = d["title"] as! String 
    print("Title:\(title) + body:\(body)") 
    self.showAlertAppDelegate(title: title,message:body,buttonTitle:"ok",window:self.window!) 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background 
    print("Handle push from background or closed\(response.notification.request.content.userInfo)") 
} 

func showAlertAppDelegate(title: String,message : String,buttonTitle: String,window: UIWindow){ 
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.default, handler: nil)) 
    window.rootViewController?.present(alert, animated: false, completion: nil) 
} 
// Firebase ended here 

} 
+0

您可以編輯您的文章僅包括相關的代碼?例如,我看到一些空的功能,可以刪除,以使文章簡潔 – jhhoff02

回答

0

剛把FIRApp.configure()放在didFinishLaunchingWithOptions的第一行。

此外,檢查推送功能是否「有效」,並且有效的權利。包括Googleservice-Info.plist文件和上傳.p12證書到AppRecord的Firebase控制檯,

希望你能得到你的解決方案。

+0

我仔細檢查了一切......它確實發送信息給設備的所有用戶,但不是單個設備。 – Phil

0

試試這個,

打開app.xcworkspace文件,選擇目標> Capabilities->推送通知,鑰匙扣共享和背景modes->遠程通知

+0

是的,完成了。 – Phil

+0

您是否可以檢查設備日期,因爲設備日期必須設置爲當前日期。 –

+0

你是什麼意思? iOS最新的? – Phil