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?
Related
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 .
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.
I'm trying to send push notifications (gcm) using example from Google Cloud Messaging for Android Library. extras/google/samples/
I set up server and i can register device on it, it returns registration id but doesn't push notification to mobile device.
things i have changes from sample: server - api key (browser/server tried both)
on client side: SERVER_URL and SENDER_ID
on html page that is included with demo example after run application : 1 device(s) registered!(with logs i can see that id and looks correct), after push Send Message : Sent message to one device: [ messageId=0:1369738939369676%921c249af9fd7ecd ]and it doesnt show up on mobile device.
What could be a problem, about what did i forgot?
i was able to read response from gcm (i guess) :
MulticastResult(multicast_id=6144555349590101172,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1369749519814767%921c249a00000031 ]]
Make sure you have proper auth token, in your server implementation.
String authToken = "AIzacq9Twg20j6g-sas............";
sender = new Sender(authToken);
This AuthToken should be taken from GCM portal and it should be mapped to SENDERID, you should use Sender ID in android app.
problem was caused bcs of firewall and blocked ports 5228-5230, admin unblocked that ports and all works like a charm