Android push notifications
Migrate Google Cloud Messaging (GCM) to Firebase Cloud Messaging (FCM) Tutorial For Android
Firebase Cloud Messaging (FCM) is the new version of GCM. https://developers.google.com/cloud-messaging/
So when you are migrate GCM into FCM first you need to add fcm things into your android project.
Follow these steps on firebase.
Here i am going to explain only how to push messages into android mobile app from particular server. For testing purpose you can use postman software instead of server. Here i am explaining using postman.
To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.
1. Create a Firebase project in the Firebase console, https://console.firebase.google.com/.
2. Click Add Firebase to your Android app and follow the setup steps same as firebase web site.
3. Make sure you have correctly located google-services.json file
1. Create a Firebase project in the Firebase console, https://console.firebase.google.com/.
2. Click Add Firebase to your Android app and follow the setup steps same as firebase web site.
3. Make sure you have correctly located google-services.json file
4. If you can't see the app folder, You can change project structure and see app folder.
5. Now you need to start coding same as firebase reference.
Then after your server side should be like this,
- Android app side you need to remove all GCM things from android application.
Now you should have proper two classes for tracking FCM notification.
So your MyFirebaseInstanceIDService.Java class should be like this,
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "FbIDService";
AsyncTask<Void, Void, Boolean> shareRegidTask;
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You must implement this method to store the token on your server
}
Your MyFirebaseMessagingService.java class should be like this(Here i am mentioning onMessageReceived method only)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
fcmMsgBody = String.valueOf(remoteMessage.getData().get("body"));
fcmMsgTitle = String.valueOf(remoteMessage.getData().get("title"));
fcmMsgIcon = String.valueOf(remoteMessage.getData().get("icon"));
requestId = String.valueOf(remoteMessage.getData().get("requestId"));
//Call this method to send notification
sendNotification(fcmMsgBody, fcmMsgTitle, fcmMsgIcon, requestId);
}
All you have done. Here i need to mention one important thing. FCM having two types of messages.
1. Notification Messages.
2. Data Messages.
Here we need to use Data Messages. Because if you are going to use Notification Messages, your received message will not work properly. When you are read documentation you can see notification messages only work for app is in a foreground. Otherwise incoming message not work properly. That's why server side i am using "data" message.
Good Luck.
private static final String TAG = "FbIDService";
AsyncTask<Void, Void, Boolean> shareRegidTask;
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You must implement this method to store the token on your server
}
Your MyFirebaseMessagingService.java class should be like this(Here i am mentioning onMessageReceived method only)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
fcmMsgBody = String.valueOf(remoteMessage.getData().get("body"));
fcmMsgTitle = String.valueOf(remoteMessage.getData().get("title"));
fcmMsgIcon = String.valueOf(remoteMessage.getData().get("icon"));
requestId = String.valueOf(remoteMessage.getData().get("requestId"));
//Call this method to send notification
sendNotification(fcmMsgBody, fcmMsgTitle, fcmMsgIcon, requestId);
}
All you have done. Here i need to mention one important thing. FCM having two types of messages.
1. Notification Messages.
2. Data Messages.
Here we need to use Data Messages. Because if you are going to use Notification Messages, your received message will not work properly. When you are read documentation you can see notification messages only work for app is in a foreground. Otherwise incoming message not work properly. That's why server side i am using "data" message.
Good Luck.
Comments
Post a Comment