我构建并测试了一个使用 FCM 的应用程序,但是当我第一次运行该应用程序时,我没有收到推送通知。但从第二次运行开始,通知开始出现。
应用程序向服务器发送token的流程如下。
测试结果,应用程序没有收到通知的情况如下。
首次运行应用程序并进行会员注册。 = 没有通知。
首次运行应用程序以完成会员注册并关闭和打开应用程序。 = 通知来了。
多次关闭和打开应用程序,然后进行会员注册。 = 没有通知。
多次关闭和打开应用程序,注册会员,然后关闭和打开应用程序。 = 通知来了。
如果我在没有推送通知的情况下进入主屏幕,我会收到通知,但当我触摸它时,didReceive函数不会执行。
即使多次关闭和打开终端,注册会员时都会发送一次令牌,但我真的不知道为什么会出现这些结果。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
// 앱 메세지를 받는 델리게이트
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
messaging.token { token, _ in
guard let token = token else {
return
}
let defaults = UserDefaults.standard
defaults.set(token, forKey:"userFCMToken")
print("FIR Token : ", token)
}
}
AppDelegate 接收 FCM 令牌的部分。接收用户通知同意的部分是按下另一个 ViewController 中的按钮。
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { success, _ in
guard success else {
print("알림 미동의")
self.registerViewPush()
return
}
print("알림 동의")
self.registerViewPush()
}
这是按下按钮时接收通知同意的部分。
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
// 어플 실행중일 때 화면에 보여주면서 실행
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent App Push")
let userInfo = notification.request.content.userInfo
print("============willPresent================")
print(userInfo)
print("============================")
completionHandler([[.alert, .badge, .sound]])
}
// 사용자가 메세지를 눌렀을 때
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("didReceive ")
let userInfo = response.notification.request.content.userInfo
print("============OriginaldidReceive================")
print(userInfo)
print("============================")
completionHandler()
}
}
我使用的 FirebaseMessaging 是 9.0.0,我使用 pod 安装它。
[
AnyHashable("google.c.fid"): f9lzDdLFjkQpneQsfb-GxW,
AnyHashable("google.c.sender.id"): 900007830000,
AnyHashable("type"): CRING,
AnyHashable("aps"): {
alert = {
body = "message";
title = "message";
};
sound = default;
},
AnyHashable("title"): message,
AnyHashable("google.c.a.e"): 1,
AnyHashable("push"): N,
AnyHashable("body"): message,
AnyHashable("gcm.message_id"): 1657080600087000
]
如果成功,这就是我将收到的。
如果答案不正确也没关系。如果您能让我知道我应该检查什么,我将不胜感激。
如果您已经拥有最新的 Firebase SDK 版本,那么您正确地错过了注册远程通知,如下所示:
UIApplication.shared.registerForRemoteNotifications()
请求授权后使用
func requestAuthorization(options: UNAuthorizationOptions = []) async throws -> Bool
这是我的错误。我希望其他开发者觉得这个答案有帮助。