About send notification to other app through FCM [duplicate] - android

I have an Android app, and I want it be able to receive push notifications from two different Firebase projects.
I read the blog "Working with multiple Firebase projects in an Android app" https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html which talks about "Accessing the Databases from two different Firebase projects".
However, there is no info about receiving notifications from multiple Firebase projects.
So, how to integrate my app with multiple Firebase projects and receive push notifications from them?

There is actually a part in the documentation about this topic:
Receiving messages from multiple senders
FCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an article aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, FCM gives you the ability to let each of these contributors send its own messages.
To make this possible, make sure each sender generates its own sender ID. See the client documentation for your platform for information on on how to obtain the FCM sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.
Finally, share the registration token with the corresponding app servers (to complete the FCM registration client/server handshake), and they'll be able to send messages to the client app using their own authentication keys.
Note that there is limit of 100 multiple senders.
I think the confusing but important part here is:
When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.
In other terms, you'll have to call getToken() passing the Sender ID and simply "FCM" (e.g. getToken("2xxxxx3344", "FCM")) as the parameters. You'll have to make sure that you call this for each sender (project) that you need.
Also, note from the getToken() docs:
This is a blocking function so do not call it on the main thread.
Some additional good-to-knows:
It does not auto retry if it fails like the default one.
It returns an IOException when it fails.

I had some issues when implementing the accepted answer and hence went ahead and tried to do it on my own when I came to find a robust solution. I have shared in detail there solution here.

Related

Receive push notifications on one android app from two Firebase projects

I have an Android app, and I want it be able to receive push notifications from two different Firebase projects.
I read the blog "Working with multiple Firebase projects in an Android app" https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html which talks about "Accessing the Databases from two different Firebase projects".
However, there is no info about receiving notifications from multiple Firebase projects.
So, how to integrate my app with multiple Firebase projects and receive push notifications from them?
There is actually a part in the documentation about this topic:
Receiving messages from multiple senders
FCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an article aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, FCM gives you the ability to let each of these contributors send its own messages.
To make this possible, make sure each sender generates its own sender ID. See the client documentation for your platform for information on on how to obtain the FCM sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.
Finally, share the registration token with the corresponding app servers (to complete the FCM registration client/server handshake), and they'll be able to send messages to the client app using their own authentication keys.
Note that there is limit of 100 multiple senders.
I think the confusing but important part here is:
When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.
In other terms, you'll have to call getToken() passing the Sender ID and simply "FCM" (e.g. getToken("2xxxxx3344", "FCM")) as the parameters. You'll have to make sure that you call this for each sender (project) that you need.
Also, note from the getToken() docs:
This is a blocking function so do not call it on the main thread.
Some additional good-to-knows:
It does not auto retry if it fails like the default one.
It returns an IOException when it fails.
I had some issues when implementing the accepted answer and hence went ahead and tried to do it on my own when I came to find a robust solution. I have shared in detail there solution here.

FCM notifications sent to topics not received anymore

My Android client app does not receive any Firebase push notifications targeting topics, however I immediately receive notifications sent to all app users or notifications sent to specific devices.
I didn't change anything in my code and I checked whether the client is correctly subscribed to topics.
For further details about my subscription logic:
In order to make it easy for my web service to send notifications to a specific user, each user is subscribed to a topic entitled with his user-id whenever he logs in from the client app.
Is this approach weak somehow? Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
It is highly suggested that developers save the generated registration token for each device for later use. As mentioned in the docs:
After you've obtained the token, you can send it to your app server and store it using your preferred method.
In your case, it is preferable. It'll remove the added action of subscribing the device to a topic. Plus it can be useful to track the message status using Diagnostics tool should you need it in the future.

Android Parse SDK not picking up deviceToken

I have a self hosted parse-server, when i create a new android user the device token in the database doesn't get set. as a note i am using fcm for push
has anyone got FCM to work with parse server?
I dont think this is possible. Reading from the Pushwoosh website who replaced Parse (since Parse was acquired by Facebook):
GCM Android Push notifications, which are being replaced by FCM now,
require you to create a Project in Google Developer Console and use
your own Sender ID and Server Key. In order to simplify the
configuration process, Parse offered to use their internal Sender ID
and Server Key to send push notifications. If you used the default
Parse Android push configuration settings and didn’t register your own
GCM sender ID and GCM Server key, all your GCM tokens belong to Parse.
This is what we never did at Pushwoosh and we always asked our users
to register their own GCM Sender ID and GCM Server Key. What is
important to understand is that Parse will never share his Private
Server Key as it would compromise all Android push tokens from any
Parse-based project.
What Does It Mean?
It means that you can use the default Sender ID and API key registered
in Parse only to send push notifications via Parse. You can’t migrate
these device tokens to another push service since you don’t have
Parse’s Server Key. In that way these tokens can be considered gone.
The only solution – you should start collecting new push tokens as
soon as you can.
In short you have to use only one.
i actually found a solution.
via https://firebase.google.com/support/guides/parse-android#firebase-notifications
so effectively there is a fix, parse uses the old way of collecting the token via gcm request token method, fcm now auto calls for a token at the time the application first installs. this creates a mismatch hence parse doesn't actually work well with fcm sdk.
but you can actually use them together and although both gcm and fcm get different tokens both these tokens actually point to the same device. i have tested this and it does work well using both tokens from a private parse server.
you must ensure you are using the "ParsePushBroadcastReceiver" or either you use a custom class as the link says. thing is you "FirebaseMessagingService" class and you custom gcm receiver class will both fire when a message comes in. so what i did was receive messages with the FirebaseMessagingService class and filter as required.

Push Notification to Specific Logged In User using Firebase/Batch

I'm building an app that allows the user to create events and add people to those events. It stores the event and certain user data (email and display name) in a Firebase database.
What I'm trying to do is send a notification to a specific user when another user adds them to an event. I've tried looking at the documentation for both Firebase Notifications/Cloud Messaging and Batch, but I can't find a way to send the notification to a single user, rather than an entire group.
How do I go about doing this? I'm hoping to avoid storing the notification in the Firebase DB and setting up a servlet backend that queries the DB repeatedly.
Edit: If I do have to make a Java Servlet backend for this, can someone tell me how I would code it?
I think you can do that by specifying the device token when sending the notification using console or using post request to FCM server. But unfortunately you have to handle every single device token manually by yourself (get it from the device when it register to FCM, and save it to your own DB somewhere).
If you use console, you can put the token on "FCM registration token" field under target -> single device.
If you use post request, you can make a request like this
{
"data": {"my_custom_key" : "my_custom_value"},
"registration_ids": ["your-device-token","your-device2-token"]
}
Batch.com provides a simpler API named Transactional API to send a notification to one or more users identified by their User ID, Firebase UID etc. as you probably don't want to deal with the complexity of APNS push tokens or GCM registration ids yourself. Contrary to FCM, you will get a complete analytics view (eg: open-rate) on those notifications.
You can trigger push notifications directly from your app code by using our Swift client for the Transactional API or by coding your own HTTP client (it's a standard POST request with a JSON body). We even have a client for Node.js if you decide to trigger the notifications from a server.
And if you are using Firebase, we have a specific documentation to plug Firebase with Batch.

Send push notifications via Topic to all devices

I'm new to push notifications and here's what I understood so far -
Client app will need to register with Google Cloud Connection Server (GCCS).
GCCS will return a registration ID back to the client.
Client will send the device id and registration ID to the app server.
App server will store device & registration in its database. This database could get huge depending on the number users.
The app server in my case comprises of a single HTML page, and a node.js script. Admin will get to the page, types in a message and hits Send.
The app server will make a POST call to GCCS with the Sender ID, API Token, array of registration ID's etc.
Message is received by the devices.
There is also this thing called as Topics that the client apps can subscribe to. Using Topics, you do not have to send registration ID's of all devices. All devices "subscribed" to this Topic will get the message. This is very useful if you have millions of users (depending on the popularity of the app) and you don't have to split the registration ID's into chunks of 1000s to send the message.
My questions are -
If we're using Topics, do we need to persist the Registration ID, Device ID to a database on the App server? It seems redundant.
Are there any ready-to-use/commercial GCM servers that can send Topic messages?
Is it easy enough to build on your own (since its a simple POST call)? If I'm going to use JavaScript, wouldn't I run into CORS issue?
Thanks for the help!
If we're using Topics, do we need to persist the Registration ID, Device ID to a database on the App server? It seems redundant.
Are you referring to C2DM implementations? Those are deprecated as shown on the GCM documentation. In relation to topics, they may not be necessary but are necessary to other message calls.
Based on the documentation, to needs to be set as topic/[yourTopic] with the necessary payload
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
}
Are there any ready-to-use/commercial GCM servers that can send Topic messages?
I can't find any sources about this, so I'm guessing we're left to implement it on our own application server. Google provided us with a sandbox project for us to play with.
Is it easy enough to build on your own (since its a simple POST call)? If I'm going to use JavaScript, wouldn't I run into CORS issue?
If you're talking about setting up the Client App (specifically Android), there's a setup guide on the documentation. But if its more on the application server, I can't comment on it really. XMPP libraries can be used to handle XMPP Connection Servers

Categories

Resources