I have written a test app where users can publish some data (location, photos, tags etc.) to their "feed" (i.e. write some data to an online database which is currently Firebase). If other users are "following" this user (or a relevant tag), how do I send notifications to the followers that new data is available?
I have looked at Google Cloud Messaging and the replacement Firebase Cloud Messaging, but I find their documentation too technical for me (seems aimed towards I.T. professionals). I can program but don't really understand "servers", "protocols" etc. It seems from GCM/FCM docs I need a "server" before I can use their services. This server will
Recognise when a database event happens.
Run some logic to ascertain whether notifications need to be sent out (and to whom)
Inform GCM or FCM to do so.
Is this correct? If so I don't know how to get a server, write one, plug one in or whatever it is I'm supposed to do with one. Any advise or simple tutorials that anyone can give a neophyte developer is greatly appreciated!
Thanks,
Riz
It seems you're trying to implement GCM Topic Messaging where your app server sends messages to mulitple devices that are subscribed to a particular "topic". Though this particular feature doesn't seem to be that feasible, it is doable.
Before your followers can get notified, they must first be subscribed to a topic.
Here's how to subscirbe in Android:
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null);
}
}
Try this actual demos to get a grip on this:
GCM Playground - A reference implementation of a GCM Application Server in the form of a playground that developers can use to test the Google Cloud Messaging Service.
Since Google now encourages users to use FCM, try the FCM Android Quickstart. You can also read through this SO post for additional insight.
Related
I would like to push some notification with a android app. So i did some researchs, but i'm little lost.
It's possible that I'm wrong but i think my only possibility is to use FCM with HTTP Request like that :
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
But i see everyone use their own database and i don't know why they need to use it. I would like not to have to use my own database, so if you have a better solution please help me.
Thank you for your all responses
(sorry for my english)
Sending a message to a device with Firebase Cloud Messaging requires that you specify the FCM server key. It is the value in the Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA header in your sample.
But a key concern here is that anyone with your FCM server key can send messages to all users of your app without restrictions. And if you embed that key into your app, it's only a matter of time before someone discovers is and uses it to send unwanted messages to your users. That's why, as its name implies, the FCM server key should only be used in trusted environments (such as a server/device you control, or Cloud Functions).
So while it is technically possible to send a messages from one Android device to another, doing so exposes users of your app to risks that you should not want.
Some more links to good previous answers and articles:
This answer from Diego one of the engineers who works on FCM.
My blog post describing the above in more details and how to send messages between Android devices securely.
The Firebase documentation example on sending messages securely from Cloud Functions
Some of my many answer on sending device-to-device messages with FCM.
I had this same problem a while back... To cope with sending notifications via FCM without a database, I used Firebase Cloud Functions and the Firebase Admin SDK to do this. I needed to send a notification when the user received a chat from the group or from another person. I used one of the 4 triggers in Firebase Cloud Functions. The one I used was the onUpdate() trigger. There are also onWrite(), onDelete(), and onCreate. All of these triggers make it possible to send a notification whenever there is a such change.. I used the Admin SDK then to get the token(if it was a single user, which I had previously uploaded to the database) or the topic(which for group chats, I named after the group chat so it would be easy to get). From this, I also named the type so it would know whether to send to a single user or a group. This allowed me to have a listener to send notifications without needing a database of my own. All through firebase. And another fact to add is that this is done in Node.JS, but it is fairly easy to grasp the concept with a knowledge of Firebase.
Some useful links:
Getting Started with Cloud Functuons
Realtime Database Triggers
Admin SDK Setup
Admin Database Getting Started, next pages for read and write(remember to use Node.JS)
Hope this helps!
I am already obtaining successfully the Firebase Cloud Messaging (FCM) device token by using FirebaseInstanceId.getInstance().getToken(). I used the following code to subscribe the client app to a topic:
String topic = "toronto";
FirebaseMessaging.getInstance().subscribeToTopic(topic);
I assume everything is correct but in order to confirm, I would like to have a Graphical User Interface (GUI) provided by FCM to manage, monitor and see all of the devices that are subscribed to specific topics. With the code I used, I would expect to see the topic "toronto", and at least one device subscribed to that topic, for example by showing the FCM device token.
I was reading at https://developers.google.com/instance-id/reference/server#get_information_about_app_instances how it is possible to get information about app instances to find out details about a client app instance's subscriptions, including each topic name and subscribe date, but does FCM provide a GUI to see that?
UPDATE 1:
I was able to subscribe devices to topics and unsubscribe devices from topics. But when I want to confirm/monitor that a device was subscribed successfully, I am using this for example (for privacy I changed values of keys and tokens):
C:\curl>curl -X GET -k --header "Authorization: key=[My key]" "https://iid.googleapis.com/iid/info/[My device token]?details=true"
{"applicationVersion":"22","connectDate":"2017-12-05","attestStatus":"NOT_ROOTED","application":"com.[My app]","scope":"*","authorizedEntity":"[My app ID]","rel":{"topics":{"San-salvador":{"addDate":"2017-12-05"}}},"connectionType":"WIFI","appSigner":"[My signature]","platform":"ANDROID"}
C:\curl>
I was expecting Firebase Cloud Messaging to provide a Graphical User Interface with a dashboard, charts and reports to see the topics that have been created and the list and number of devices subscribed to each of the topics, something similar to the Google Analytics reports, maybe even with maps to see where devices are subscribing from or anything visual that can help to visualize and monitor topic subscriptions. Instead, I am having to do everything with cURL with code similar to what I am showing in this UPDATE 1. I am surprised Firebase Cloud Messaging does not provide a GUI, since the tool comes from Google and they could easily provide a dashboard or something similar to Google Analytics.
FCM currently doesn't have a GUI that displays the list/count of topics you have or the subscribers (count/registration tokens) the topic has.
At most, you could use the Instance ID API (the one from your post) to check a single registration token to which topics it is subscribed to. However, do note that the Instance ID API was meant to be used on your Server side.
Other than that, you will have to implement your own mapping with the topics (which tokens are subscribed to it).
Kinda similar/possibly helpful posts:
Firebase Cloud Messaging - Check existing or available Topics
Count subscribers of a topic in Firebase Cloud Messaging
Android/Firebase - Check to see if you are subscribed to topic
Firebase Cloud Messaging - Managing Registration Tokens
I am having trouble understanding the specific meaning of "app server implemented in my own environment" as used in this documentation on how to send upstream messages to the "Cloud" using Firebase Cloud Messaging.
Context
The documentation is saying that for me to send upstream messages, I need my own app server that implements one of two connection server protocols in HTTP or XMPP.
The Reason why I am confused
My expectation is that if I use Firebase, I don't need to create my own server. All of the backend stuff is handled by them. So to me, when they say I need to create my own app server in my own environment, it is contrary to my expectation and understanding and thus makes me second guess the meaning.
What it is I am specifically confused and asking about
What exactly do they mean by app server?
What exactly do they mean by "in my own environment"?
Another way my two questions could be asked is:
Is an app server in this context meaning just a typical app server that I write on my own using something like Node.JS/Express.JS and host on something like Digital Ocean? and/or
Is it something I need to do with other Firebase/Google Cloud products (eg Hosting or App Engine)?
Or could my implementation of the RealTime Database feature on my app be considered an "App Server" as it serves my app with data.
What I have done so far
Reading through every single documentation on Firebase relating to Cloud Messaging and browsing around Google.
I have had a solid read of what questions to ask and what to avoid. I am mindful that this question could be something with lots of varying and all correct answers so apologies if that offends one of the rules. But I have come across this question on Meta which suggests that asking for clarification on documentation is OK for SO.
Is an app server in this context meaning just a typical app server that I write on my own using something like Node.JS/Express.JS and host on something like Digital Ocean?
Exactly.
Is it something I need to do with other Firebase/Google Cloud products (eg Hosting or App Engine)?
Depends on your use-case. But for Firebase Cloud Messaging, when all you need is to send Downstream Messages, you don't need an App Server. You can just make use of the Firebase Console. If you need to send Upstream Messages, then you have to implement your own App Server.
Or could my implementation of the RealTime Database feature on my app be considered an "App Server" as it serves my app with data?
Not exactly. The Firebase Realtime Database stores the data you need, but the App Server needed for FCM is something that can process requests (Send (Downstream and Receive (Upstream)).
App notifications are sent by Firebase Cloud Messaging server. To send these notifications you will have to tell to which device notifications have to be sent.
So you will have to send the FCM Client ID from your backend server (Node.js, PHP server). You will get the FCM client ID when you integrate Firebase into your app. You will have to store the FCM client ID in your backend server like during registration of a user.
To send a notification to a device retrieve the FCM Client id from your backend server and send it to the Firebase cloud messaging server and it will check if the client id is valid and trigger a notification.
I'm currently working on an application build on AWS. At first, the application was on Parse server and the push notifications were not a problem but then, when I migrated to AWS I encountered some issues with that.
As far as I read from this link http://aws.amazon.com/articles/9156883257507082 I learned that I need to create a topic to subscribe users by creating endpoints using the device tokens from where they are currently logged in.
I will have many users, and the notifications will be sent to all endpoints from a topic.
The problem is when I want to send a notification I want to exclude some users from a topic for receiving that, for example, when a user posts something I will not send a notification to him too. This is available also when I have a custom audience to send notifications to. I could create a topic for every event notification to be sent but I don't think this is a efficient method.
Is there a possibility to do that or Amazon does not even support this feature? Until now I didn't find anything on internet that can help me solve this problem and I hope I'll find here someone who worked with this.
Thank you for your time!
Plus : I also found a similar question here Send Push Notifications using Amazon SNS service but I still don't have a certain answer or some links about that.
From what I've found in AWS documentation, it states that
Send messages directly to a specific device by calling the Publish function with the device’s ARN. You can easily scale this to handle millions of users by storing the endpoint ARNs in Amazon DynamoDB and using multi-threaded code on the server.
Thus, to be able to filter users that receive notifications and not send bulk messages to all users subscribed to a topic, you need to send messages using the device's ARNs.
You can find more info here
I am new to gcm api for android and have for some time now i have being working on an android app to allow chatting between two users of the app. The app is such that a chat can only be initiated when one user opts to contact the other user. But my confusion comes in the manner i would be able to create a chatroom for these two users and for the other user to be able receive messages. since i found out that each user must subscribe to a topic inorder to receive messages in that topic. Would i have to subscribe all users to all possible topics or what? that is my big question but it seems it would have so much overhead considering i have 1000+ users.
Please i need all the help i can get here. Thanks
Would i have to subscribe all users to all possible topics or what?
GCM topic messaging allows your app server to send a message to multiple devices that have opted in to a particular topic.
It is not a requirement but it can ease the work for the server to send messages. In this tutorial, you will see that they have created a chat like environment using GCM without using the topic function.
BUT consider the effects on your server like how will it behave on the potential load when you use the topic messaging, especially the the message will trigger an interaction from the user to the server.