Python Firebase Admin SDK appears successful but I never receive the notification

我们两清 提交于 2021-01-27 16:42:42

问题


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

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?


回答1:


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

  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.

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.




回答2:


The return value is a message ID string, and currently it doesn't point to anything (i.e. not a valid URL). It's just the unique ID that FCM has assigned to your notification, and it indicates the notification has been successfully handed off to FCM for delivery. Also, I believe that notifications sent from the SDK do not appear in the Firebase console. You need an actual client (Android, IOS or web) to test this flow end-to-end.



来源:https://stackoverflow.com/questions/53771076/python-firebase-admin-sdk-appears-successful-but-i-never-receive-the-notificatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!