Reputation: 5193
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?
Upvotes: 8
Views: 7138
Reputation: 480
I had this problem for a week. But it was resolved. Follow these:
[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.
Upvotes: 7
Reputation: 7438
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.
Upvotes: 1