Enable Android Application With Firebase Cloud Service
Posted By : Keshav Gupta | 28-Apr-2018
Steps to make on server side:
1. Go to firbase console.
2. Now add a project.
3. Select platform in which you want to add firebase.
4.Now add package name of your project in window and you can add other options over there too.
5.After this "google-services.json" will be generated. Download this
6. Switch to the Project view in Android Studio to see your project root directory.
7. Move the google-services.json file you have just downloaded into your Android app module root directory.
Now in Android Application:
Project-level build.gradle (<project>/build.gradle
):
buildscript
{
dependencies
{
classpath
'com.google.gms:google-services:3.1.0'
}}
App-level build.gradle (<project>/<app-module>/build.gradle
):
Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
//add firebase sdk in dependency:
compile 'com.google.firebase:firebase-messaging:9.4.0'
Create two classes name whatever you want:
1.MyFireBaseInstanceIdService
2.MyFireBaseMessagingService
//--------------------------------------------------MyFirebaseInstanceIdService--------------------------------------------//
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private SessionManager sm;
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
sm=new SessionManager(getApplication());
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("TAG", "Refreshed token: " + refreshedToken);
//saving registration token in shared prefernces
sm.setSaveRegisterationToken(refreshedToken);
}
}
//--------------------------------------------------------//
//--------------------------------------------------MyFirebaseMessagingService--------------------------------------------//
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "FIREBASE";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());
String body=object.optString("body");
String title=object.optString("title");
String notificationType=object.optString("notificationType");
sendnotification(body,title,notificationType);
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification: " + remoteMessage.getNotification());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
//sendnotification(mesg);
}
public void sendnotification(String mess,String title,String noti_type) {
// TODO Auto-generated method stub
NotificationManager mNotificationManager = (NotificationManager)getApplication()
.getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap largeicon= BitmapFactory.decodeResource(getResources(), R.drawable.applogo);
PendingIntent pendingIntent = null;
Intent intent=null;
intent=new Intent(getApplication(),SplashActivity.class);
pendingIntent = PendingIntent.getActivity(getApplication(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setLargeIcon(largeicon);
mBuilder.setSmallIcon(R.drawable.applogo);
mBuilder.setContentTitle(title);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mess));
mBuilder.setContentText(mess);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Keshav Gupta
Keshav Gupta is Android Developer in Oodles, he always look forward for new tasks and new things to learn more.