gpt4 book ai didi

ios - 如何从 iOS 上的 Firebase 云消息传递获取 token

转载 作者:行者123 更新时间:2023-11-28 07:52:55 25 4
gpt4 key购买 nike

播客文件

target 'T' do
use_frameworks!

pod 'Firebase/Core'
pod 'Firebase/Messaging'
pod ‘Firebase’
pod ‘Firebase/Auth’
pod 'Firebase/Database'
pod 'Firebase/Storage'
end

在 AppDelegate.swift 中导入

import UIKit
import CoreData
import Firebase
import UserNotifications
import FirebaseMessaging

AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate

运行Xcode 9.2,编译语言版本swift 4,部署目标11.2

根据文档https://firebase.google.com/docs/cloud-messaging/ios/client我应该可以使用以下内容

1) Messaging.messaging().delegate = self
2) func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}

3) FIRMessageDelegate

但是xcode无法识别- 来自 1 的“消息”类)-'FIRMessageDelegate' 来自 3)- 或 2) 中的委托(delegate)方法

因此我无法获得我的应用所需的 fcmtoken

Xcode 确实识别这些但这些不在文档中所以我不知道该怎么做

4) FIRMessagingDelegate
5) FIRMessaging.messaging().remoteMessageDelegate = self

4)中唯一的委托(delegate)方法是

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage)
{
print(remoteMessage.appData)
}

我的问题是:如何获得 token ?该文档似乎已过时。

podfile.lock

PODS:
- Firebase (3.15.0):
- Firebase/Core (= 3.15.0)
- Firebase/Auth (3.15.0):
- Firebase/Core
- FirebaseAuth (= 3.1.1)
- Firebase/Core (3.15.0):
- FirebaseAnalytics (= 3.7.0)
- FirebaseCore (= 3.5.2)
- Firebase/Database (3.15.0):
- Firebase/Core
- FirebaseDatabase (= 3.1.2)
- Firebase/Messaging (3.15.0):
- Firebase/Core
- FirebaseMessaging (= 1.2.2)
- Firebase/Storage (3.15.0):
- Firebase/Core
- FirebaseStorage (= 1.1.0)
- FirebaseAnalytics (3.7.0):
- FirebaseCore (~> 3.5)
- FirebaseInstanceID (~> 1.0)
- GoogleToolboxForMac/NSData+zlib (~> 2.1)
- FirebaseAuth (3.1.1):
- FirebaseAnalytics (~> 3.7)
- GoogleToolboxForMac/NSDictionary+URLArguments (~> 2.1)
- GTMSessionFetcher/Core (~> 1.1)
- FirebaseCore (3.5.2):
- GoogleToolboxForMac/NSData+zlib (~> 2.1)
- FirebaseDatabase (3.1.2):
- FirebaseAnalytics (~> 3.7)
- FirebaseInstanceID (1.0.9)
- FirebaseMessaging (1.2.2):
- FirebaseAnalytics (~> 3.7)
- FirebaseInstanceID (~> 1.0)
- GoogleToolboxForMac/Logger (~> 2.1)
- Protobuf (~> 3.1)
- FirebaseStorage (1.1.0):
- FirebaseAnalytics (~> 3.7)
- GTMSessionFetcher/Core (~> 1.1)
- GoogleToolboxForMac/DebugUtils (2.1.1):
- GoogleToolboxForMac/Defines (= 2.1.1)
- GoogleToolboxForMac/Defines (2.1.1)
- GoogleToolboxForMac/Logger (2.1.1):
- GoogleToolboxForMac/Defines (= 2.1.1)
- GoogleToolboxForMac/NSData+zlib (2.1.1):
- GoogleToolboxForMac/Defines (= 2.1.1)
- GoogleToolboxForMac/NSDictionary+URLArguments (2.1.1):
- GoogleToolboxForMac/DebugUtils (= 2.1.1)
- GoogleToolboxForMac/Defines (= 2.1.1)
- GoogleToolboxForMac/NSString+URLArguments (= 2.1.1)
- GoogleToolboxForMac/NSString+URLArguments (2.1.1)
- GTMSessionFetcher/Core (1.1.9)
- Protobuf (3.2.0)

最佳答案

在 appdelegate 中试试这个

import UIKit
import Firebase
import UserNotifications






@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {

var window: UIWindow?
var fcmTokenUser : String?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

FirebaseApp.configure()


print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)


// To get FCM token that will be sent to APNS via Google FCM
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()


Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken





return true
}



func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
// This callback is fired at each app startup (when the user install the app for the very first time) and whenever a new token is generated due to The app is restored on a new device, The user uninstalls/reinstall the app, The user clears app data.

// after fcm generated for the very first time,then fcm can also be retrieved in the 'didFinishLaunchingWithOptions' method above (let token = Messaging.messaging().fcmToken)


fcmTokenUser = fcmToken






}


private func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}





}

连播文件

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for HRIS

pod 'Firebase/Core'
pod 'Firebase/Messaging'
pod 'FirebaseInstanceID'






end

关于ios - 如何从 iOS 上的 Firebase 云消息传递获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225714/

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