2017-07-14 247 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