iOS FCM推送通知未在第一个运行应用中收到

发布于 2025-02-13 11:32:31 字数 3452 浏览 0 评论 0原文

我构建并测试了使用FCM的应用程序,但是当我第一次运行该应用程序时,我不会收到推送通知。但是从第二次运行开始,通知就开始了。

将令牌发送到服务器的应用程序的过程如下。

  1. 该应用程序已启动。
  2. 将令牌存储在UserDefault中。
  3. 获得知情同意。
  4. 继续会员注册。
  5. 会员注册结束时,会员信息和令牌被一起发送。
  6. 成员资格注册是

由于测试完成的,通知不会来自该应用程序的情况如下。

  1. 首次运行该应用程序并继续进行会员注册。 =无通知。

  2. 首次运行该申请,以完成会员资格注册并关闭并在应用程序上关闭。 =即将到来的通知。

  3. 关闭申请并进行几次关闭,然后进行会员注册。 =无通知。

  4. 关闭申请并在几次上关闭,注册会员资格,然后关闭应用程序。 =通知即将到来。

如果我在没有推动通知的情况下输入主屏幕,我会收到通知,但是触摸它时不会执行底处理功能。

即使终端被关闭并且几次关闭,在注册成员时也会发送一次令牌,但我真的不知道为什么这些结果会出来。

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

接收FCM令牌的AppDelegate的一部分。从用户接收通知同意的零件是按下另一个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()
}
}

我正在使用的Firebasemessging是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
]

如果这成功,这就是我将收到的。

如果答案不正确,没关系。如果您能让我知道我应该检查什么,我会很感激。

I build and test an app that uses FCM, but when I run the app for the first time, I don't get a push notification. But from the second run, notifications start to come.

The procedure for the application to send the token to the server is as follows.

  1. The application is launched.
  2. Store the token in UserDefault.
  3. Get informed consent.
  4. Proceed to membership registration.
  5. At the end of membership registration, member information and token are sent together.
  6. Membership registration completed

As a result of the test, the situation in which notifications do not come from the app is as follows.

  1. Run the app for the first time and proceed with membership registration. = No notification.

  2. Run the application for the first time to complete membership registration and turn off and on the application. = A notification is coming.

  3. Turn the application off and on several times and proceed with membership registration. = No notification.

  4. Turn the application off and on several times, sign up for a membership, and turn the application off and on. = Notification is coming.

If i enter the home screen without a push notification, I get a notification, but the didReceive function is not executed when I touch it.

Even if the terminal is turned off and on several times, the token is sent once when registering as a member, but I really don't know why these results are coming out.

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

The part of the AppDelegate that receives the FCM token. The part to receive notification consent from the user is to press a button in another ViewController.

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { success, _ in
        guard success else {
            print("알림 미동의")
            self.registerViewPush()
            return
        }
        print("알림 동의")
        self.registerViewPush()
    }

This is the part that receives notification consent when the button is pressed.

@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()
}
}

The FirebaseMessaging I'm using is 9.0.0 and I installed it using a 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
]

If this is successful, this is what I will receive.

It's okay if the answer isn't right. I'd appreciate it if you could let me know what I should check.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2025-02-20 11:32:32

如果您已经拥有最新的firebase SDK版本,那么您会正确缺少注册远程通知如下:

UIApplication.shared.registerForRemoteNotifications()

在请求授权后,

func requestAuthorization(options: UNAuthorizationOptions = []) async throws -> Bool

我的错误是我的错误。我希望其他开发人员发现此答案有帮助。

If you already have latest Firebase SDK version, then you properly missing registering for remote notifications as following:

UIApplication.shared.registerForRemoteNotifications()

after requesting authorization using

func requestAuthorization(options: UNAuthorizationOptions = []) async throws -> Bool

This was my mistake. I hope other developers find this answer helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文