There is some troubles with pushes delivery, so recently we decided to gather FCM Aggregated Delivery Data according to this post and see insights.
1 - I followed steps described in BigQuery data export and linked project to BigQuery
2 - Then configured android project export to Enable message delivery data export
...
FirebaseApp.initializeApp(context)
FirebaseMessaging.getInstance().setDeliveryMetricsExportToBigQuery(true)
...
Android project uses in app-module level build.gradle
implementation platform('com.google.firebase:firebase-bom:30.0.0')
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
After configuring project it started to collect data in BigQuery without analytics_label. However the query to view aggregated push delivery data were returning empty data field:
{
"androidDeliveryData": [
...,
{
"appId": "...",
"date": {
"year": 2022,
"month": 12,
"day": 25
},
"data": {}
}
]
}
Our backend were using legacy API to send data notifications. And we diceded to add analytics_label to sending push notification from backend, they said that this field is in new Firebase API v1. Backend team succesfully migrated to firebase-api-v1 from legacy. And BigQuery were populated with analytics labeled data. According to limitaions of API it can take yp to 5 days to be able to see aggregated data. It's been 9 days and I still getting empty data fields.
How can I achive getting aggregated pushes delivery data?
Related
I would like to send in-app and cloud messages to a group of users based on non-standard criteria.
I decided to link firebase project with BigQuery and use imported segments to accomplish this task.
The problem is that fcm message composer always target 0 users when i filter users based on the segment.
I added such entry to the SegmentMetadata table
{
"segment_label": "test_segment_label",
"display_name": "test_display_name_metadata"
}
and such entry to the SegmentMemberships table
{
"instance_id": my device fcm token,
"segment_labels": [
"test_segment_label"
],
"update_time": null,
"membership_name": "test"
}
Now on the firebase console when i am trying to send cloud message and target users based on this segment i see that 0 users are matched, do you have any idea why?
image snapshot from firebase console
(my device fcm token that i putted on the entry in the SegmentMemberships table should be valid because i see on android studio logs that it doesn't change)
instance_id is not your fcm token but the part before colon in your token.
When you call getToken() in SDK the token returned is in this format: <instance_id>:<token>
I am trying to send push notifications using python to all users. However, I am aware that there is no way to do this using apps and you have to use topics (as far as I am aware).
Is there a way that I can create a topic out of the app?
Thanks
Edit: I am completely new to firebase (so sorry if I am difficult)
First of all you need to understand a topic does not need to create (it will be create automatically), you only need to define the topic name for example if you are creating app to receive push notification when the weather change, so the topic name could be "weather".
Now you need have 2 components: mobile & backend
1. Mobile: in your mobile app you only need integrate the Firebase SDK and subscribe to the topic "weather" how do you do that?
Firebase.messaging.subscribeToTopic("weather")
Don't forget checking documentation.
2. Backend: in your server you will need to implement the sender script based on FCM SDK.
If you are a beginner I'd recommend you use Postman to send push notifications and then integrate FCM in your backend app.
You can send this payload trough Postman (don't forget set your API KEY in headers)
https://fcm.googleapis.com/fcm/send
{
"to": "/topics/weather",
"notification": {
"title": "The weather changed",
"body": "27 °C"
}
}
If that works, you can add FCM SDK to your backend:
$ sudo pip install firebase-admin
default_app = firebase_admin.initialize_app()
Finally you can send notifications as documentation says:
from firebase_admin import messaging
topic = 'weather'
message = messaging.Message(
notification={
'title': 'The weather changed',
'body': '27 °C',
},
topic=topic,
)
response = messaging.send(message)
More details here: https://github.com/firebase/firebase-admin-python/blob/eefc31b67bc8ad50a734a7bb0a52f56716e0e4d7/snippets/messaging/cloud_messaging.py#L24-L40
You need to be patient with the documentation, I hope I've helped.
The above solutions are depreciated and outdated.
Let me include the latest implementation of firebase-admin SDK for python.
import firebase_admin
from firebase_admin import credentials, messaging
cred = credentials.Certificate(
"<path-to-your-credential-json>")
firebase_admin.initialize_app(cred)
topic = 'notification'
message = messaging.Message(
notification=messaging.Notification(
title='The weather changed', body='27 °C'),
topic=topic,
)
response = messaging.send(message)
print(response)
*Note of few configurations:
Obtain your credential.json in your firebase console under: "Project settings" -> "Service accounts" -> "Generate new private key"
Make sure you subscribe to the correct topic name for both of your server and client application. Every client applications that subscribed to the same topic regardless of which devices will received the corresponding notifications.
Have a good day~
To subscribe an Android client to a topic, do as shown in the documentation on subscribing to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("weather")
Then you can send a message to that topic from a trusted environment, such as your development machine, a server you control, or Cloud Functions. For an example of this, see How do you send a Firebase Notification to all devices via CURL?
Our app now has targetSdkVersion 26 (Android 8) and the app uses FCM push notifications.
As FCM documentation prescribes I updated the FCM client library to version 11.2.0:
dependencies {
compile 'com.google.firebase:firebase-messaging:11.2.0'
}
With this FCM client library update the FCM notifications started to appear on Android devices. Good, but when app is in background it's system who processes the FCM message, so it uses the default Android notification channel named "Miscellaneous", which is not what we want (we have other notification channels and "Miscellaneous" sounds confusing in that list).
As FCM documentation says there is a way to specify default notification channel for FCM messages:
(Optional) Within the application component, metadata elements to set
a default icon, color and notification channel (new in Android O) for
notifications. Android uses these values whenever incoming messages do
not explicitly set icon, color or notification_channel.
However there is no code sample shown (samples are shown only for icon and color). So I just found by googling a sample in Firebase Cloud Messaging Quickstart on github:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="#string/default_notification_channel_id"/>
But it does not work - FCM notifications still appear within the "Miscellaneous" channel. And I see in the logs:
W/FirebaseMessaging: Missing Default Notification Channel metadata in
AndroidManifest. Default value will be used.
Of course, I tried to reinstall the app. Still having the issue.
Well, ideally there should be some way to specify notification channel(s) on back-end at the moment of sending the messages. The FCM dev console, which allows to test sending, now has such an option in UI:
And it works fine. However our back-end uses Java Amazon SNS API and I have no idea if that API allows to specify Android notification channel when sending a message (because it'a new Android feature, and Amazon needs time to adopt it). So setting a default notification channel in AndroidManifest.xml would be a valid workaround for now, but it does not work.
Look at docs: https://firebase.google.com/docs/cloud-messaging/http-server-ref
android_channel_id The notification's channel id (new in Android O).
The app must create a channel with this ID before any notification
with this key is received.
If you don't send this key in the request, or if the channel id
provided has not yet been created by your app, FCM uses the channel id
specified in your app manifest.
Try to include android_channel_id in json you are about to post to fcm. I have no idea why manifest value is not working for you. Try to just add channel to your request, you should get the same effect as from Firebase Console.
Edit: I just realized you are asking for Amazon client integration. Maybe you are able to build json request manually then (I don't know much about Amazon services, sorry).
FCM has migrated to HTTP v1 API:
https://fcm.googleapis.com/v1/projects/{{projectId}}/messages:send
android_channel_id will cause bad request:
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.notification",
"description": "Invalid JSON payload received. Unknown name \"android_channel_id\" at 'message.notification': Cannot find field."
}
The correct payload should be:
{
"message": {
"token": "{{deviceToken}}",
"notification": {
"body": "This is an FCM notification message hello 23",
"title": "FCM Message",
"image": "https://lumiere-a.akamaihd.net/v1/images/au_moviesshowcase_mulan_poster_r_2_54011055.jpeg?region=0,0,960,1420"
},
"android": {
"notification": {
"channel_id": "channel_id_1"
}
},
"data": {
"key1": "42",
"key2": "sent by 21"
}
}
}
see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message
Depend on this resource : https://firebase.google.com/docs/cloud-messaging/http-server-ref
Here Payload look like :
{
"to":"$device_token"
"notification":{
"title":"Title",
"body":"Here Body",
"android_channel_id":"$channel_id", // For Android >= 8
"channel_id":"$channel_id", // For Android Version < 8
"image": "https://xxxxx.com/xxxxx.jpeg"
},
"data":{},
"priority":"normal"
}
this is my first time posting a question to stackoverflow, so I will do my best to expand on my question in a clear manner. I've beat my head against the wall in trying to find an answer, but am currently stuck, so any insight offered would be greatly appreciated.
I am trying to send FCM messages to an Android app that I've developed. I followed the Android Quickstart tutorial: https://firebase.google.com/docs/cloud-messaging/android/client
I am able to send Notification messages to my app from the Firebase Cloud Messenging composer, and the notifications are properly accepted by the app and when the app is visible, loads the data payload in a simple Activity -> TextView.
Now, I am trying to push the notifications from a Google App Engine server app using the Python SDK, built upon the Webapp2 framework, and I am unclear how to go about doing this.
In a RequestHandler, I want to push data received from a POST request to the Notification. I have written some basic code in an attempt to do this:
class SendDetails(webapp2.RequestHandler):
def post(self):
url = 'https://fcm.googleapis.com/fcm/send'
body = {
"data": {
"title": "mytitle",
"body": "mybody",
"url": "myurl"
},
"notification": {
"title": "My web app name",
"body": "message",
"content_available": "true"
},
"to": "AIzaSyAqv4hjGC1Z5.......XyQ",
}
headers = {"Content-Type": "application/json",
"Authorization": "AIzaSyDhIji4X6h0VQwor.......lrr2zo"}
try:
form_data = urllib.urlencode(body)
headers = {"Content-Type": "application/json",
"Authorization": "key=AIzaSyDhI.......mOUXlrr2zo"}
result = urlfetch.fetch(
url='https://fcm.googleapis.com/fcm/send',
payload=form_data,
method=urlfetch.POST,
headers=headers)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(result)
except urlfetch.Error:
logging.exception('Caught exception fetching url')
When I run the code on dev_appserver, and trigger the RequestHandler via a browser, I get a "POST HTTP/1.1 200 68" response. I am unsure how to interpret the "68" code.
Depending on how I format the request, sometimes I am redirected to the
"Firebase Cloud Messaging HTTP Protocol" webpage at https://firebase.google.com/docs/cloud-messaging/http-server-ref which tells me that I am on the right track.
Next, in order to gain some insight, I have tried sending Notifications from a Chrome app called Advanced REST Client (ARC) which allows me to build HTTP requests and forward to the FCM server.
Here is an example of what I send to the FCM server:
Content-Type: application/json
Authorization: key=AIzaSyDhIji4X6h0........2zo
{"to": "AIzaSyAqv4hjGC1Z5........cfSRU_gXyQ",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}}
I get the following response:
{
"multicast_id":
7345322951926226000
,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
],
}
I am sure that I am missing something simple, but as a newcomer to AppEngine and Firebase, I am not sure where I am going wrong.
Well, after much further investigation, the answer was (as usual) right under my nose, but not quite so obvious.
For my testing, I was using the legacy server key assigned by Firebase/Google and not the newer one that they recommend -- even though it clearly states in the Firebase Console / Messenger section that it is still useable. As soon as I plugged in the new key, everything worked as intended.
I am trying to add new subscriber with pushwoosh remote api services.
RegisterDevice
POST https://cp.pushwoosh.com/json/1.3/registerDevice
{
"request": {
"application": "XXXX-XXXX", // THE PROJECT ID ON PUSHWOOSH
"push_token": "APA........", // GOOGLE DEVICE REGISTRATION KEY
"language": "en",
"hwid": "YYYYYYYYY", //UUID
"device_type": 3 //ANDROID
}
}
// RESULT 200 - OK
My app works perfectly fine.
when i check my pushwoosh control panel i see subscribers
but the Android platform shows no subscribers.
I cross checked the project id , server key... and everything fine.
what am i doing wrong?
Check the server response. It should be 200 and status code is "ok".
Sometimes when the Cloud is super busy it might take a minute or two for the devices to appear.