Notifications using firebase for android studio - android

I'm making an app that requires to book people for a specific purpose. I would like to notify the person that he is being booked by someone. I can't figure out how this is done.
Examples of sending notifications from the server to everyone who has the app is available but that is not what i want.
Once someone clicks the book button from his account, the other person should get a notification. Please help!

You have to follow below steps for achieve this same.
Step 1: Need to store token from "FirebaseInstanceId" when user signup or signin and also store that token on web server.
Step 2: Need web-service where we can send that "Book" button selected user userId through web-service. After that web-service working on that request, find TOKEN from server of that same user and set notification payload with
{ to: "token", ... }
and then send notification with some intial details using FCM.

Step 1 : Create account in FCM
Step 2 : Create app in FCM Console
step 3 : Get Token id of device and send to server.

Related

sending a notification to other users when a user clicks a button using firebase

I am using Firebase for my android studio project and I want to have a notification button so that whenever a user clicks a send button, the notification button indicates there is a new notification and that notification contains the data that the user sent. Is it possible to do so using firebase ? and how ?
Sure. You want to use Topic Messaging in FCM.
Here's the setup guide.
Once implemented, subscribe a user with:
FirebaseMessaging.getInstance().subscribeToTopic("dogs")
And send a notification via a POST request on the app (insecure, not recommended) or via a server (recommended):
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":{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
I don't know, whether you figured it out or not (because you asked the question months ago), but I may have an idea. Actually I was also searching for what you asked to do, and I read your question. I wanted to do the same thing as you. Then I thought, if firebase cloud messaging service can receive http POST request not just from firebase console but also from any other service like ARC (Advanced REST client).
So the solution :
Send a JSON HTTP post from your app when the users click on the app with the required data received from user. Now when the JSON response is received by the firebase cloud messaging server, it should you the notification to every user with the data sent by the user who clicked the button.
I haven't tried it yet but I am going to try it now. I will let you know in the comments if it works or not. It should work theoretically. If you have already figured out a way to send such notifications, please don't forget to share your method.
If you want to know how to send JSON HTTP post, then here is a stack overflow answer : SENDING JSON OBJECT

Push notification for android app by GCM

I am working on android app for events reminder , this app only display event as list and details about it .. some event's date is change .
I need to push notification to remind a user about event date
I'am Confused when read about Google Cloud Messaging ( GCM )
Is it necessary to request from the user to enter name or email through the application ? I don't need that !
where can I write a new notification "message" to send it ?
Thank you ,
The requirement is simple.
You need to generate gcm token from each of your client.
Client A installs your application and upon launch/login(wherever it fits in your business logic) you will try to generate this gcm token(let us say 10000 is the generated token) . And you need to send this token to your server to store for using it in future communication. when you want to communicate with this client A, you need to tell gcm server,
Hey Gcm server, send mobile with gcm token 10000 the following message "Event time updated"
So if client b installs, it will generate its gcm token and send it to server in same way.
In case you have user login or some way where each user can be identified uniquely, it is recommended to store it along with user details. (along with name, unique id, etc... )
If you do not have login, you can still send token to server and store but what you loose out is the cases where You want to send update to Client A but not B.
Coming to code point of it.
Client code:
For generating gcm token:
https://github.com/googlesamples/google-services/tree/master/android/gcm
after you generate you need to send this token to server
Server code:
server receives the token and stores.
When you want to send a particular message in later point of time, you can send json data or plain message to client.
For example, Following is the python example.
import gcm
response = gcm.json_request(registration_ids=reg_ids, data=in_data, delay_while_idle=False)
Hope this helps.

not sure about GCM implention

My website api send a gcm notification to user in case of he received a normal texting message from another user.
Google docs explain the implention steps like i have been coding android for 100 years, but in fact im a nope lvl5 so i didnt understand it.
In addition all web tutorial explain the old way,
thus what i did is:
1-created a new app on Google console and downloaded that service.json file and add the menifest and gradle things, no errors now all good,
2-then i went to official Github gcm project and copied every thing into my project without any changes except that the Mainactivity content of Github project i added it to the login activity of my project,
still no error, but am i doing it right?.
The second question is:
when user login i should send the user devise id to my server along with username and passowrds params,
How to get that from shered prefenince.
Soory for the long story and thanks in advance.
This is the github project
https://github.com/googlesamples/google-services/tree/master/android/gcm
for the first time when an app is launched the app registers in gcm and recieves token and it is stored in sharedpreference with a keyword. look for the gcm registering in your launching activity. then use tis key to retrive the token from sharedpreference in login activity
goto this url
https://github.com/googlesamples/google-services/blob/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart/RegistrationIntentService.java
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
you have to store value in token in sharedpreference
then retrieve it in login activity and send it to your server

Android PushNotification From Json URL

I have a Json URL, which contains data about Latest Job Postings, I am successfully parsing the Json URL and able to display the top job postings in my ListView.
But my requirement is to create a push notification, so that whenever a new job is posted, the user should be able to get a notification on device.
I have followed this: http://www.vogella.com/articles/AndroidNotifications/article.html
But I don't know how to get notifications in my case.
Could anyone help me?
Issue:
Give push notification to user's device about the updated data even when application is in background mode.
Solution:
Upon successful insertion of new data in your database (which is going to give updated set of data to your JSON request) , just call the file which send GCM push notification to all your users.
Reference:
GCM docs
GCM push-notification using php server
In context of implementation presented in demo app of 2nd link,
upon successful insertion,you can call send_message.php file,but make sure that $regId and $message should be retrieved from your database
You have created ActionBar Notifications for your app, but now you need to create the ability to receive notifications from a web client, instead of going to find them yourself from the URL.
To create a push notification you would need to have a constant thread (BroadcastReceiver) on the device that is waiting for the notification from the sever.
Google 'Cloud to Device Messaging' is the simplest way to do this.
This is a good link with lots of info on how to do this :
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/
If you require these notifications to be displayed on the device even when the application is not running (which seems to be the case from what you describe), you can use Google Cloud Messaging.
You would need a server that would poll the Json URL for updates, and send a GCM message to all the devices where your app is installed once such an update is detected.
Your app would have to register to Google Cloud Messaging and send the Registration ID received from Google to your server.
When your app receive a GCM message, you would create a notification and when the notification is tapped, you would start the activity that loads the data from the JSON URL.

Android : Some Questions to Understand GCM well

I am Trying to fetch and push A notification (To inform the device to connect to the data source because it has been changed/Updated)
I read A lot of tutorials about it , and tried to apply it to help me in my case , but really i faced some problem understanding the GCM HOW IT Works
I've register at https://code.google.com/apis/console and take the SENDER ID AND APIKEY
Now I've Some Questions :
do you register the device to the service every time we start the application or only for the first time ?
do we use GCM for (only notify the device something happens) or to (notify and get the information about whats is changes) ?
why this operation need (Client And Server)
I mean (Client) it's OK Because he'll receive the push notification ,, but (Server What it Means Here) :( ,, because i think the server here will be the Google it self to notify the device
In this Case When We Use A APIKEY
the Idea Is :
I've SQL Server Database on my web-site and have small android application (Dealing With it) with two users
need when any user Add A record to the database notify the other user
Thanks In Advance ,,
Regards :)
1) You register with GCM only when you need to. E.g. When you call
GCMRegistrar.getRegistrationId(appContext);
And it returns a blank string. Typically this happens the first time you launch your app.
2) It depends on what the data you are trying to is and whether or not it will fit within the 4K payload limit. Take a look at the Send-to-Sync vs. Messages with Payload Google Help topic.
3) Your server is the server that sends the message to the GCM servers. The GCM servers then send the message down to the apps. Take a look at this blog post I wrote for a service that I helped create: http://blog.andromo.com/2012/how-does-airbop-push-messaging-work/ It should help explain how this works. In the explanation you can just substitute our servers with your own.
4) You send your API key as part of your message to the GCM servers as detailed here: http://developer.android.com/google/gcm/gcm.html#request
So in the following request:
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
Authorization: key=YOUR_API_KEY

Categories

Resources