ios - 函数 didReceive remoteMessage 未被调用 Swift 5
问题描述
因此,我一直在这里和其他地方阅读有关此主题的其他帖子,但我仍在苦苦挣扎。我的应用程序正在使用"Fireabase FCM"
. 我能够发送和接收消息。但是,该方法"didReceive remoteMessage"
永远不会被调用。我想使用这个函数来更新应用程序和标签栏项目徽章编号。
我尝试添加其他方法来指示已收到消息,但这些方法也没有奏效。我想我错过了一些基本的东西。我正在Swift 5
使用Xcode 10
.
import Foundation
import UIKit
import Firebase
import FirebaseFirestore
import FirebaseMessaging
import UserNotifications
class PushNotificationManager : NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {
static let shared = PushNotificationManager( )
var userID: String = ""
var instanceIDToken : String = ""
let defaults = UserDefaults.standard
override init( ) {
super.init()
}
func setUserId(identifier: String) {
userID = identifier
}
func registerForPushNotifications() {
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)
Messaging.messaging().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
UIApplication.shared.registerForRemoteNotifications()
UNMutableNotificationContent().sound = UNNotificationSound.default
Messaging.messaging().shouldEstablishDirectChannel = true
updateFirestorePushTokenIfNeeded()
}
func getNotificationSettings( ) {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {return}
DispatchQueue.main.async{UIApplication.shared.registerForRemoteNotifications()}
}
}
func updateFirestorePushTokenIfNeeded( ) {
// let mytoken = Messaging.messaging().fcmToken
if let token = Messaging.messaging().fcmToken {
let usersRef = Firestore.firestore().collection("users_table").document(userID)
usersRef.setData(["fcmToken": token], merge: true)
}
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
func application(received remoteMessage: MessagingRemoteMessage) {
print("applicationReceivedRemoteMessage")
print(remoteMessage.appData)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("REMOTE NOTIFICATION RECEIVED")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
updateFirestorePushTokenIfNeeded()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print(response)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func getTokenDirectly( ) {
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instance ID: \(error)")
} else if let result = result {
print("Remoted instance ID token: \(result.token)")
self.instanceIDToken = result.token
self.updateFirestorePushTokenIfNeeded()
}
}
}
}
同样,我在用于测试的设备上收到通知,但是
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
永远不会被调用。
解决方案
看来您没有调用此方法:
registerForPushNotifications()
因为那是您将您设置delegate
为self
委托的方法的地方,因此不会调用
推荐阅读
- r - 构建 R 文档时,Unicode 连字符会导致错误
- hyperledger-fabric - 八卦跳过握手与双向 TLS
- javascript - 如何通过 jquery 中的用户输入更新 json 中的值?
- php - 如果字段 = 回声...否则
- git - 无法解析主机名:从 CircleCI 作业推送到 Github 私人仓库时
- pandas - 如何根据另一列的条件对每一列求和而不迭代熊猫数据框中的列
- sql - 如何将字符串文字添加为选择查询的一个选定列?
- java - 如何将模式/其他用户添加到给 java 的数据库 url?
- kotlin - 检查 map 是否包含子字符串。科特林
- sql-server - 如何在 SQL 中指定主键起始编号