首页 > 解决方案 > 没有在我的 iPhone 上收到推送通知,但 Firebase Functions 说它已成功发送?

问题描述

我希望 Firebase 在用户收到新消息时发送推送通知,它曾经可以工作,但由于 FCM 令牌的一些错误没有保存给新用户,我不得不更改一些代码。但是,现在它将正确的 FCM 令牌保存给已注册的新用户。

但是,当用户现在收到一条消息时,没有显示任何通知......我检查了我在 firebase 函数上的日志,它说消息已成功传递(因此我认为这是我的代码中的错误)。

这是我在 AppDelegate 中获取用户 FCM 令牌的方式:

func application(_ application: UIApplication, 
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
  {
       InstanceID.instanceID().instanceID { (result, error) in
        if let error = error {
            print("Error fetching remote instange ID: \(error)")
        } else if let result = result {
            print("Remote instance ID token: \(result.token)")
            self.tokenOfUser = result.token

        }
    }
  }

这就是我在用户注册时在 SignUpViewController 中检索令牌的方式:

  var deviceToken : String?
  var delegate = UIApplication.shared.delegate as! AppDelegate
  self.deviceToken = delegate.tokenOfUser

  //and in saving the user profile i add this: 
  ["fcmToken": deviceToken]

这是我在 FeedViewController 中的通知代码

这是用户注册后的第一个视图控制器。

override func viewDidLoad() {
    super.viewDidLoad()
    registerForRemoteNotifications()
  }


func registerForRemoteNotifications() {
    Messaging.messaging().delegate = self
    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 })
    } else {

        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, 
            .sound], categories: nil)

    UIApplication.shared.registerUserNotificationSettings(settings)

    }

    UIApplication.shared.registerForRemoteNotifications()


     }

func messaging(_ messaging: Messaging, didReceiveRegistrationToken 
fcmToken: String) {



    print("Firebase registration token: \(fcmToken)")
    //UserDefaults.standard.set(fcmToken, forKey: "fcmToken")



}

我知道这些功能没有问题,因为它曾经完美地传递通知(所以这些功能以前可以工作)

我希望 Cloud Functions 向我的设备发送推送通知(在存储在我的数据库中的 FCM 令牌的帮助下)。

标签: swiftxcodefirebasefirebase-cloud-messaginggoogle-cloud-functions

解决方案


发现了问题。我将 FirebaseAppDelegateProxyEnabled 设置为 NO。

我将其更改FirebaseAppDelegateProxyEnabled为 YES ,现在它工作正常!


推荐阅读