gpt4 book ai didi

iOS Firebase 推送通知 : How To Give Firebase User's Device Token And Send Notification

转载 作者:IT王子 更新时间:2023-10-29 08:13:18 26 4
gpt4 key购买 nike

最近在 Google I/O 事件上,Google 更新了 Firebase 并添加了许多新功能,并对其余功能进行了修改。我一直在尝试通过最基本的级别通过 Firebase 将 iOS 推送通知实现到我的应用程序中,所以我创建了一个非常简单的应用程序,除了接收远程推送通知之外什么都不做。

在 Firebase 中,我上传了我的证书,在 Xcode 中,我的配置文件已添加到目标和项目中,在 Firebase 中,我上传了正确的证书。下面是包含在我的 AppDelegate.swift 文件中的代码,但是因为我的 ViewController.swift 是“空的”,所以我没有包含它。

虽然没有崩溃或运行时错误,但当我加载应用程序时,我接受了通知。然后,我退出应用程序并关闭我的设备。在 Firebase 中,我将通知发送到正确的应用程序。几分钟后,在 Firebase 中显示通知“已完成”。

但是,我从未在设备上收到通知。因此,总而言之,我需要一个解决方案来向 Firebase 发送此 deviceToken,然后使用“Firebase Notifications”发送推送通知消息。

任何对我的代码或一般情况的帮助都将不胜感激,我希望这对 future 的观众有所帮助。谢谢!我在 AppDelegate.swift 中的代码:

import UIKit
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

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

FIRApp.configure()

let notificationTypes : UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]

let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)

application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)

return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

print("Device Token: \(deviceToken)")

}

func applicationWillResignActive(application: UIApplication) {

}

func applicationDidEnterBackground(application: UIApplication) {

}

func applicationWillEnterForeground(application: UIApplication) {

}

func applicationDidBecomeActive(application: UIApplication) {

}

func applicationWillTerminate(application: UIApplication) {

}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

print("MessageID : \(userInfo["gcm.messgae_id"]!)") // or gcm_etc...

print(userInfo)

}


}

最佳答案

更新:从 Firebase 4.0.4 开始,您可以关注 https://github.com/onmyway133/blog/issues/64

如何处理 APNS 设备 token

我一直在阅读 Send a Notification to a User Segment on iOS但是没有提到对推送通知至关重要的 APNS 设备 token 。

因此,Firebase 一定在进行一些内部调整。事实上它是。阅读后端文档 Downstream Messages给了我们想法

Swizzling disabled: mapping your APNs token and registration token

If you have disabled method swizzling, you'll need to explicitly map your APNs token to the FCM registration token. Override the

methods didRegisterForRemoteNotificationsWithDeviceToken to retrieve the APNs token, and then call setAPNSToken.

func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenTypeSandbox)
}

我特别尝试尽可能避免混合。读书Migrate a GCM Client App for iOS to Firebase Cloud Messaging告诉我们如何禁用它

启用/禁用方法调配

Method swizzling available with FCM simplifies your client code. However, for developers who prefer not to use it, FCM allows you to disable method swizzling by adding the FIRMessagingAutoRegisterEnabledflag in the app’s Info.plist file and setting its value to NO (boolean value).

FCM swizzling affects how you handle the default registration token, and how you handle downstream message callbacks. Where

applicable, this guide provides migration examples both with and without method swizzling enabled.

给我看代码

将其放入您的 Podfile

pod 'Firebase'
pod 'FirebaseMessaging'

这里是完整的代码

import Firebase
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)
}

关于iOS Firebase 推送通知 : How To Give Firebase User's Device Token And Send Notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37667753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com