Python Firebase Admin SDK似乎成功,但我从未收到通知 [英] Python Firebase Admin SDK appears successful but I never receive the notification

查看:147
本文介绍了Python Firebase Admin SDK似乎成功,但我从未收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python Firebase Admin SDK为我的移动应用发送推送通知.我已经在Firebase控制台中的通知编写器上对其进行了测试,因此我知道我的设备可以接收推送通知,但是当我尝试使用SDK时却什么也没收到.我也看不到Firebase控制台通知页面上列出的通知.

I'm attempting to use the Python Firebase Admin SDK to send push notifications for my mobile app. I've tested it with the notification composer in the Firebase Console, so I know my device can receive push notifications, but when I try to use the SDK I never receive anything. Nor do I see the notification listed on the Firebase console notification page.

这是我用来发送通知的确切代码(减去我的个人信息):

Here's the exact code(minus my personal info) that I'm using to send the notification:

import firebase_admin
from firebase_admin import credentials, messaging

token = "registration token"
creds = credentials.Certificate('path/to/cert.json')
app = firebase_admin.initialize_app(creds)
msg = messaging.Message(data={'title': 'Test'}, token=token)
print(messaging.send(msg, app=app))

这将返回一个遵循/project/< project name>/messages/< message ID> 格式的URL,但该URL对我不起作用.它将仅将我重定向到Firebase控制台基本域和黑屏.另外,通知应该列在/notifications 下吗?

This returns a URL that follows the format /project/<project name>/messages/<message ID> but that URL doesn't work for me. It will just redirect me to the Firebase console base domain and a blank screen. Also, the notifications should be listed under /notifications shouldn't they?

推荐答案

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

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.

这篇关于Python Firebase Admin SDK似乎成功,但我从未收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆