We are planning to wirte a messaging/chat kind applicaiton on Android.We are planning to use GCM to exchange messages.The traditional way is to have a Application Server to store all the gcmid of the users and if user1 wants to send message to user2,
1.The user1 send the message to Application server with payload which contains message and receipent id i.e user2
2.The application server retrieves the gcmid of user2 and call sender.send(regid2 , message)
3.User2 receives the message.
I see it is just a REST API Call to https://android.googleapis.com/gcm/send.
So if somhow user1 has the gcmid of user2 why can't we directly call the gcm API from user1 device to reduce the load on server and cost of operations on server.Pls advise me on this.
Note:We are not planning to use Upstreaming
i did the same using Device_To_Device_Messaging_Using_Google_Cloud_Messaging_GCM_-_Android_Example tutorial. this tut is having complate sever as well as client code using gcm. go through it,will help you to achieve what you want.
So if somehow user1 has the gcmid of user2 ...
I think this is precisely where the difficulty lies. If you call directly the GCM API from the device, you need to transfer the gcmid from one device to the others, and by far the simplest and scalable solution I can think of is to use a server, reachable from every devices.
Related
I want to build a notification system in my Android app where I can send a notification to someone upon an event. For example, my app is about a social system where it uses Firestore database (Firebase) to save its users (which means I have a 'users' collection in there), and I have an 'Add as Friend' feature.
Everything works just fine, but I would like to add a feature where when user1 adds user2 as a friend, I would like to send user2 a notification about that.
I have searched in google and saw this post: https://www.geeksforgeeks.org/how-to-push-notification-in-android-using-firebase-cloud-messaging/
Now, from what I understand, this wouldn't help me much cause it only sends the notification from FCM by me (and not to a specific user).
The problem is, I don't really know what to search for (or what to type in google) in order to find what I'm looking for.
Some ideas I thought:
to make a collection in firestore called 'Notifications' where it holds usernames as documentIDs
when user1 sends a friend request to user2, I would add data (like title/message) to 'Notifications/user2/'
But I'm kind of stuck here, I would like to know ideas or if you guys have threads on how do I implement this, I really don't know what should I look for.
Thank you very much and sorry for the long post.
Firebase creates a userID for every user and a usertoken which is a temporary identifier for the device which is connected to this userID. You need to save the usertoken when there is a new one being created in a database(firestore) along with the user with whom you identify this person.
When someone adds this person as a friend you can add a notification in the database that there is a request from person1 to person2.
This change in the database can trigger a cloud function that reacts to changes in the database.
The triggered cloud function sends an FCM to the person which is being added as a friend. This FCM can either be a pure notification or you send data and let your app handle how this data is being handled(saved in a local database or displayed as a custom notification).
Keywords are: FCM, Notification, Firestore, cloud functions, and firebase token
Edit (Answer to 1. comment): Firebase userID and usertoken are created on the device. In the tutorial you implement FCM(I didn't read it complete) and a class called FirebaseMessagingService, from which you can inherit. This class is called, when a message is sent to the device and contains a method called onNewToken. This message is called, when the Firebase Server creates a token for your device and sends it to the device. In this method you might want to send the new token to your database and save it as an identifier for the user of this device. The Tokens are used by firebase to identify devices, to which messages are being sent(You can read about FCM in detail)
Cloud functions are something you can deploy along your firebase project, which get triggered when an event occurs. For example a http call, or a specific change in your database. Cloud functions are written in JS and need to be deployed for your firebase project. (Cloud functions are not for free and you need to pay for each call, but you have some thousand uses for free each month.)
You send a FCM to a specific person by creating a firebase message which is directed to a specific token, tokens or user groups. To identify the person to which you want to send a message you need to have the token for this user, that is why you are saving the tokens from the 1. part in your database and always update the token, when the token changes on the device.
Keep in mind, that a user might use multiple devices. You should have an identifier for you, such as username and along this username you save multiple firebaseIds and Firebase tokens. A single FCM can be directed to up to 100 tokens.
What i did is :
I have used JobScheduler. In every two hours, it checks weather there is a new data available or not in Server->Database->Table. If new data available it retrieves the data and notifies to the subscriber.
Kindly any one suggest me weather this is right approach or not?
Is there any other way to do so?
Can I implement PUB/SUB concept here..
If yes, How?
I am using Godaddy Hosing..
No,its not a good way
You may try like this
1) Integrate FCM Push notification at Backend side(PHP etc)
2) Take Legacy Server Key for Push notification from Firebase
3) Wherever you will get update of data send notification using Device Token
4)at app side you need to add code for handle push notification
for more help refer this URL
I am developing a custom chat application.
Sent messages are stored on my server using a simple POST.
A service that runs in background is responsible to check using another POST every 5 seconds if there are new messages for the current user and send a notification to the him/her.
I am a bit worried about network and CPU usages because i am aware that internet connection is used all the day.
I used that approach because i need to manipulate some data on my server code before delivery a notification to receiver.
An example is that i have some users assigned as admin. The users username is their email. When a message is sent from one of the admins i overwrite their username (so their email) with the application name before delivery the notification to the receiver.
Is this the best approach to create a custom chat application?
Dont know exactly why you used this use case, but really simple solution provides Firebase.
You can read something about that here. Basically you have to:
Create some layouts
Add listener on "send message" click, that will add message to firebase realtime database
Create FirebaseListAdapter, who will display messages to user
From database docs
all of your clients share one Realtime Database instance and
automatically receive updates with the newest data.
From my understading you dont have to check every 5 seconds if there is some message waiting. Firebase handles it for you.
We have a python script which do specific job and we want when it see the trigger occurred , it sends a push notification to android devices
but we don't want to send a default message to all of our users.
we want to send customize message based on the trigger to our users
for example :
"user1" the "trigger" hits.
"user2" the "trigger" hits.
.
.
can you help me how to send different messages to different unique android IDs?
I suggest that you're familiar with GCM (official documentation for start you could find here Set up a GCM Client App on Android
Now regarding your question:
you have to have storage on your server where you save registration id, device token + any info which will help you to differ one user/device from another. For example, I did same thing for different apps and stored package_name in addition.
To send custom message you'll need:
1) choose to which devices/users you'll send a message (on exit you'll have a list of device tokens)
2) get list of registration ids from device tokens (on exit list of registration ids)
I separated 1 and 2 for simplicity.
3) I suggest that you know that GCM supports for up to 1,000 recipients for a single message. So you should form here a string of registration ids separated by comma. If you need to send message to 2500 devices, you need to do 3 requests.
4) send request to GCM server and see custom message on specific devices.
Regards.
i have developed successfully one listview using soap webservice.here i have to insert 2 item on my database means that time (2 new item is inserted) the notification message is automatically comes to the top, clicking the notification will take you directly to the app and display listview with the 2 new items also.it is perform even my app is closed also like gmail.in gmail got any new message means that time the notification message is displayed,clicked that notification message means directly go to inbox and view that message. How can i implement this.
you can use GCM for this,
Google Cloud Messaging for Android (GCM) is a service that helps
developers send data from servers to their Android applications on
Android devices. This could be a lightweight message telling the
Android application that there is new data to be fetched from the
server (for instance, a movie uploaded by a friend), or it could be a
message containing up to 4kb of payload data (so apps like instant
messaging can consume the message directly). The GCM service handles
all aspects of queueing of messages and delivery to the target Android
application running on the target device
This document describes how to write an Android application and the server-side logic, using the helper libraries (client and server) provided by GCM.
Check these links,
http://developer.android.com/guide/google/gcm/gs.html
http://developer.android.com/guide/google/gcm/demo.html
http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/19226-android-push-notification-gcm-framework-tutorial.html
http://fundroiding.wordpress.com/2012/06/29/google-cloud-messaging-for-android-gcm-simple-tutorial/
for client side app you can find the code here
https://github.com/marknutter/GCM-Cordova
SOAP Service messqges are get only when the device requests something from the server. If you would like to use SOAP, you need to request from the server if there is a new item each amount of time (which is not really good practice). Then, if a new item is there, you can request the item by SOAP and insert this item in your array list and refresh the table.
A nicer way of doing is to use GCM (Google Cloud Messaging for Android): http://developer.android.com/guide/google/gcm/index.html
There, you can create a server side app where you will notify your device whenever a new order is inserted. You have a method onMessage() wher you can handle the message and insert it in your list.
It depends of what do you want exactly, the question can be developped more in details.