Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send notifications from one android device to another?

I'm building two apps, one for booking a service and one for the managers to respond to the services requested by the clients, as soon as a booking is made it has to detect the location and according to that it has to send the order notification to the nearest manager. How do I do this. I'm using Google maps api to search the nearest manager and that piece of code is working fine. But I'm unable to integrate the push notifications as I have no idea about it. How do I use my server to do this and once the notifications is sent, if the manager doesn't respond, then I want the server to send the message again after 30 minutes. Please help me.. Thank you!

like image 217
user3908652 Avatar asked Feb 23 '16 02:02

user3908652


1 Answers

I'm facing the same problem, so I will write my steps below.

FCM INSTALLATION

1) As google says Firebase Cloud Messaging (FCM) is the new version of GCM.. So if you are starting with a new app, use Firebase Cloud Messaging instead.

You will need:

  • A device running Android 2.3 (Gingerbread) or newer, and Google Play services 9.4.0 or newer

  • The Google Play services SDK from the Android SDK Manager Android Studio 1.5 or higher

  • An Android Studio project and its package name

2) Create a Firebase project or import existing one and follow the setup steps

FCM USE

1) To send a message to a specific device, you need to know that device's registration token. When you need to retrieve the current token, call FirebaseInstanceID.getToken(). Also don't forget to implement onTokenRefreshcallback. The onTokenRefreshcallback fires whenever a new token is generated.

@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);
}

2) After you've obtained the token, you can send it to your app server and try to send some messages.

  1. Install and run the app on the target device.
  2. Make sure the app is in the background on the device.
  3. Open the Notifications tab of the Firebase console and select New Message.
  4. Enter the message text.
  5. Select Single Device for the message target.
  6. In the field labeled FCM Registration Token, enter the registration token you obtained in a previous section of this guide.

That's in the short all you will find in documentation. Hope that it will help someone.

like image 110
kristyna Avatar answered Oct 18 '22 05:10

kristyna