【发布时间】:2017-05-30 11:08:06
【问题描述】:
我有以下场景
使用 APNS
用于接收我的原生 iOS 应用程序的远程通知。在使用它时,我们需要创建 .p12 证书,并且我们需要在注册推送通知时发送在 Appdelegate.m 文件中生成的设备令牌。因此,我们采用了将设备 ID 发送到后端的方法,将通知发送到该特定设备。
使用 FCM 时
我查看了 FCM,还得知我们需要将 .p12 文件上传到他们的控制台。到此为止一切都很好。但是当谈到设备令牌部分时,我不清楚“设备令牌”的“Swizzling”过程。 firebase 是生成设备令牌还是我们需要设置在 didRegisterforRemoteNotification 中生成的设备令牌?
import FirebaseMessaging
override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(tokenRefreshNotification(_:)),
name: kFIRInstanceIDTokenRefreshNotification,
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)
}
【问题讨论】:
-
是的,instanceID 是您的令牌,您的后端需要它向启用了 Firebase 通知的特定设备发送通知。
-
它不是在firebase中生成的,您可以在
didRegisterforRemoteNotification函数中获取设备的设备令牌。 -
@PraveenKumar 是的,一般来说你是对的,但是对于firebase通知,我们应该使用firebase生成的instanceID。
-
是的,你是对的。现在,这是您的设备令牌。
-
@AnuragSharma instanceID 是否不断刷新?如果是,如何处理?我们是否需要单独的 API 调用来发送刷新的令牌。
标签: ios firebase swift3 push-notification firebase-cloud-messaging