且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python Firebase Admin SDK似乎成功,但我从未收到通知

更新时间:2022-06-10 22:15:35

这个问题我已经有一个星期了.但这已解决.请按照以下步骤操作:

I had had this problem for a week. But it was resolved. Follow these:

  1. 确保您的 APNs证书 APNs身份验证密钥已上传到 Firebase控制台
  2. 在发送包含APN( messaging.APNSConfig )的 firebase.messaging.Message 时,请确保使用正确的 FCM令牌./li>
  3. AppDelegate.m 文件是否已设置.> Firebase文档.就我而言,我忘记在函数 application:didRegisterForRemoteNotificationsWithDeviceToken:中添加 [FIRMessaging消息传递] .APNSToken = deviceToken; .请记住,当您在服务器中发送消息时,即使您收到一条消息(该消息不包含通知),响应也没有说任何有关推送通知到iOS设备的信息.而且我也不知道如何检查通知是否已发送.
  1. Make sure your APNs Certificates or APNs Authentication Key were uploaded in Cloud Messaging settings in Firebase Console
  2. Sure that you use the correct FCM token when sending a firebase.messaging.Message which included apns (messaging.APNSConfig).
  3. Check your AppDelegate.m file was setup following Firebase Docs. In my case, I forgot add [FIRMessaging messaging].APNSToken = deviceToken; in function application:didRegisterForRemoteNotificationsWithDeviceToken:. Keep in mind that the response when you send a message in your server did not say anything about the push notification to your iOS devices even you received a message (it did not contain a notification). And I also don't know how to check whether notification delivered or not.

我的python服务器代码:

My python server code:

def push_notification():
    title = "Your title"
    message = "Your message"
    ntf_data = {"key": "value"}
    fcm_token = "your_fcm_token"
    topic = "your_topic"

    # apns
    alert = ApsAlert(title = title, body = message)
    aps = messaging.Aps(alert = alert, sound = "default")
    payload = messaging.APNSPayload(aps)

    # message
    msg = messaging.Message(
        notification = messaging.Notification(
            title = title,
            body = message
        ),
        data = ntf_data,
        token = fcm_token,
        topic = topic,
        apns = messaging.APNSConfig(payload = payload)
    )

    # send
    res = messaging.send(msg)

以及前端反应本地通知侦听器代码:

And frontend react-native notification listener code:

    onNotificationListener = firebase.notifications().onNotification((notification) => {
         console.log("received notification:", notification)
    }

随时询问我的答案是否有任何问题,或者您需要更多信息.编码愉快.

Feel free to ask if there is any problem in my answer or if you need more information. Happy coding.