I developed a client on android app and a server, but now I discovered the GMC- "Google Cloud Messaging for Android"
Is there any way to connect the GCM to my server, without changing everything?
The client send data to the server with socket, and what I need is that the server will send the data to the GCM, and the GCM will send the data to all the clients.
I don't need the clients to send data to the GCM, all what I need is :
client -(socket)--> server ---> GCM ---> clients
Someone have any idea how to do that?
Thanks in advance!
You can send GCM messages from your Java Server. The simplest way is to use the server library supplied by Google (gcm-server.jar).
The code for sending a message is as simple as :
Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
.delayWhileIdle(true)
.addData("key1", "value1")
.addData("key2", "value2")
.build();
Result result = sender.send(message, registrationId, numOfRetries);
In addition, you'd have to check the result, to see if your message was accepted or rejected by GCM server.
This example shows how to send a message to a single device. There's a similar way to send the same message to multiple devices.
Finally, you'll have to implement some web service that accepts a registration ID from your app, and saves it in your DB.
Related
I want to sent a message with the fcm of google but I need a class named "Message" but it doesn't exist with my imports. (I implemented com.google.firebase:firebase-messaging:18.0.0)
I tried using the remote message class but it doesn't fit to my problem.
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
I want to use this example code:
// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setTopic(topic)
.build();
// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
The needed class is this: https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/messaging/Message but it doesn't exist.
Sending messages to users/devices with Firebase Cloud Messaging, requires that your code has access to the FCM Server Key. As its name implies, this key should only be used on a server, as having access to this key allows one to send any messages to all users of your app.
You seem to be writing code in an Android app, while the Message class you're referring to is part of the Firebase Admin SDK. The Admin SDK gives its users full access to your Firebase project (including your FCM server key), and for that reason can only be used in a trusted environment, such as your development machine, a server you control, or Cloud Functions.
So to send messages to a user/device, you will always need a server, or otherwise trusted environment, to send them from. For more on this, see:
How to send Device to device notification by using FCM without using XMPP or any other script.?
my blog post Sending notifications between devices with Firebase Database and Cloud Messaging
How to send device to device messages using Firebase Cloud Messaging?
My clients are Android devices. A client wants to send some info to another client which will first be sent to the app server and then the server will notify a middleware service(some kind of message broker) which will be responsible for sending the target client a push notification(to tell it to poll from the app server).
I want to know:
How will this middleware service identify which client device to notify?
more concretely, How does a push notification server locate which physical device to fire off the notification to?
My requirement is that the client should be able to obtain notifications even while using a VPN or proxy(similar to WhatsApp). Also, if the client is powered off then the middleware queues the messages and delivers them once the client power backs on again.
I have been looking at things like Firebase Cloud Messaging and MQTT brokers and stuff but I want to implement similar things on my own.
I want to implement this in Java.
Your client ( having an unique id and token ) will send the message to another client who also have a unique id and token in the database .. So the,
Server will map the receivers' id to the receivers token and send the notification to the receiver..
The database(backend) will have unique ids (maybe primary keys) against each client and also a unique token (for sending notification) against each client. So , when the sender-client sends the message , the backend script maps the receiver-client's (to whom the sender is sending) id to the receiver clients token. Then send the notification to the particular token.
I am developing an application in android (Twilio Android Client) using Twilio, which consumes the Rest API.
I can successfully send the SMS using the rest API. However, when someone replies to SMS/MMS, the server will get that message because Twilio will hit that URL.
But on the other hand, I don't understand how to update the client device when we receive an SMS/MMS using Twilio.
Is there any way to update or send the message (SMS/MMS) to the client device when a server receives an incoming message(SMS/MMS)?
You can configure a URL for SMS/MMS in your twilio number. Now, when the user replies to this number, this script in this URL will be executed. In this script, write code to forward SMS/MMS to your android device's phone number.
If you want to handle replies solely via your app, (ie, not by conventional SMS and MMS) then store reply SMS and MMS in server. Then write an API in server to get these messages. Now write code in android to periodically query this API and retrieve these messages.
My android app uses GCM to send or receive text messages. At first, when a user install my app, it uses the GCM api to get a GCM_id from Google. After getting this long GCM id, app sends this id to my web server to say that this client is registered with that id.
My question is that about this step. Since firstly app gets the GCM id and sends it to my web server, I think, someone can listen the requests of the app and sends dummy GCM_ids for this client (or any other clients if guess my client id format). How can I secure this step to prevent unwanted interrupts and attacks.
While creating gcm_id, if google would send this id to my server also before sending it to client, it would be a solution to my problem. But google sends it to client only directly.
ps: Using ssl is not a solution I think. Because it can also be opened by programs like fiddler .
Currently, sending a message from my server gives me an OK response, meaning the message has been forwarded to GCM and it will be received by the device unless it's offline, which it will receive once it's online. Would it be possible to determine if the device hasn't received it yet because it's currently offline?
There is no way to query the status of a GCM message after it is sent. One option would be to have your app request a delivery receipt, or manually ack the message via HTTP. Then if your server doesn't get the ack within a certain time you could then send via SMS.
As you then end up sending some messages via GCM and SMS, you will need code to de-duplicate the messages in your app.
now GCM support "message delivered to device", just set "delivery_receipt_requested": true and deal eith incomming ack..
see this
It seems you are using HTTP protocol for sending messages.
If you use XMPP instead, and add "delivery_receipt_requested": true to your JSON, GCM will send a Delivery message to you upon Delivery. (you can find the documents here)
Otherwise you have to handle it manually on HTTP.