I use FCM to send message to client (android), but I will have many client android and I want to send message to only one device not to everyone. How can I do this? Now when I send a message I send to everyone
Asked
Active
Viewed 735 times
1
-
run the script using php/ any other back end languages, and limit the receiver by syncing with db. – Manoj Perumarath Mar 29 '17 at 10:59
2 Answers
1
send message to one device no to every one
When you register FCM from client app, you will receive a token for that device. You can pass it into server and save it there. Later you can use that specific device token to send FCM message to that device.
The code snippet which returns the FCM token is given below
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
see the official doc

Darish
- 11,032
- 5
- 50
- 70
1
If you have "FCM registration token" of all user you can send message to Single Device or user using that user FCM registration token

omkar ugale
- 106
- 7