本文介绍了APNS Firebase通知无法获取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ios10-swift-3-and-firebase-push-notifications-fcm> ios10,Swift 3和Firebase推送通知(FCM)

试图使用Firebase for Notifications,我完全按照文档中的描述进行了整合。
但我不明白为什么不行。当我建立我的项目,我看到这一行:

  2016-05-25 16:09:34.987:< FIRInstanceID / WARNING> ;无法获取默认标记Error Domain = com.firebase.iid Code = 0(null)

我的AppDelegate:
$ b $ pre $ func application(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {
FIRApp.configure()
FIRDatabase.database()。persistenceEnabled = true
var service:DataService = DataService()
service.start()
registerForPushNotifications (application)
application.registerForRemoteNotifications()
return true

$ b func registerForPushNotifications(application:UIApplication){
let notificationSettings = UIUserNotificationSettings(
forTypes:[.Badge,.Sound,.Alert],categories:nil)
application.registerUserNotificationSettings(notificationSettings)
}

func应用程序(application:UIApplication,didRegisterUserNotificationSettings notificationSettings:UIUserNotificationSettings){$ b $如果notificationSettings.types!= .None {
application.registerForRemoteNotifications()
}
}

func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData){
let tokenChars = UnsafePointer< CChar>(deviceToken.bytes)
var tokenString =

for i in 0 ..< deviceToken.length {
tokenString + = String(format :%02.2hhx,参数:[tokenChars [i]])
}

FIRInstanceID.instanceID()。setAPNSToken(deviceToken,type:FIRInstanceIDAPNSTokenType.Unknown)
print (Device Token:,tokenString)
}
func application(application:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject],fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) - >无效){
//打印消息ID。
print(Message ID:\(userInfo [gcm.message_id]!))

//打印完整的信息。
print(%@,userInfo)
}


解决方案1.在 didFinishLaunchingWithOptions 方法中设置通知观察器

2.并设置 tokenRefreshNotification 方法那么你得到这个方法的令牌。



见下面的代码

 导入Firebase 
导入FirebaseMessaging

覆盖func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {
FIRApp.configure()

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

//注意:当禁用swizzling时需要使用
public func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData){

FIRInstanceID.instanceID()。setAPNSToken(deviceToken,type:FIRInstanceIDAPNSTokenType.Sandbox)
}

func tokenRefreshNotification(notification: NSNotification){
//注意:在这里可以为零
let refreshedToken = FIRInstanceID.instanceID()。token()
print(InstanceID token:\(refreshedToken))

connectToFcm()
}

func conn ectToFcm(){
FIRMessaging.messaging()。connectWithCompletion {(error)in
if(error!= nil){
print(无法连接FCM。 \(error))
} else {
print(Connected to FCM。)
}
}
}

public func application(application:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject]){$ b $ print(userInfo)
}




For Swift3 / iOS10 see this link:

ios10, Swift 3 and Firebase Push Notifications (FCM)

I'm trying to use the Firebase for Notifications and I integrated it exactly as described in the docs.But I don't understand why is doesn't work. When I build my project I see this line:

2016-05-25 16:09:34.987: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"

This my AppDelegate:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
     var service: DataService = DataService()
    service.start()
    registerForPushNotifications(application)
    application.registerForRemoteNotifications()
    return true
}

func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
    print("Device Token:", tokenString)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)  {
    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)
}
解决方案

1.Set Notification Observer in didFinishLaunchingWithOptions Method

2.And Set tokenRefreshNotification method then u get Token in this method.

See below Code

import Firebase
import FirebaseMessaging

override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  FIRApp.configure()

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

// NOTE: Need to use this when swizzling is disabled
public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

  FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}

func tokenRefreshNotification(notification: NSNotification) {
  // NOTE: It can be nil here
  let refreshedToken = FIRInstanceID.instanceID().token()
  print("InstanceID token: \(refreshedToken)")

  connectToFcm()
}

func connectToFcm() {
  FIRMessaging.messaging().connectWithCompletion { (error) in
    if (error != nil) {
      print("Unable to connect with FCM. \(error)")
    } else {
      print("Connected to FCM.")
    }
  }
}

public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
  print(userInfo)
}

这篇关于APNS Firebase通知无法获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:06