Hello friends,
In the first part of this series we learn how to register our application on Firebase Console. In second part we learn about firebase authentication. In this part we will learn how to send push notification to our android application using Firebase.
As we know we can send push notifications using Google Cloud Messing(GCM) too. But configuring our application for GCM to receive push notifications is complex. In compare to GCM configuring push notification in our application is very easy. You have to just register you application on firebase console and create you application structure successfully. If you don't go through that how to register our application to Firebase Console, please go through this link, which is the first part of this series describe how to register our application to Firebase Console.
If you have done with application configuration, then you 80% done. Now you have to just create two service classes, one extending FirebaseMessagingService and second extending FirebaseInstanceIdService. Now register your created service classes to you manifest.xml. You are done all with configuration for firebase push notification.
NOTE: I will not explain how to implement it on server side. I will so
example to sending notification using Firebase
Console.
Ok, Now add this gradle line to your build.gradle file:
compile 'com.google.firebase:firebase-messaging:9.2.0'
Create two service classes One for getting notification
token id, which is used for server side notification implementation and Second
for receive notifications.
MyFirebaseInstanceIDService.java
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
public MyFirebaseInstanceIDService() {
}
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("firebase", "Refreshed token: " + refreshedToken);
//This token is used by server side so use below method to send token to server
// sendRegistrationToServer(refreshedToken);
}
}
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
public MyFirebaseInstanceIDService() {
}
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("firebase", "Refreshed token: " + refreshedToken);
//This token is used by server side so use below method to send token to server
// sendRegistrationToServer(refreshedToken);
}
}
MyFirebaseMessagingService.java
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import com.doird.amar.myfirebaseimpl.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("firebase", "Notification Message: " +
remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getBody());
}
private void showNotification(String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Firebase Push Notification");
builder.setContentText(msg);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import com.doird.amar.myfirebaseimpl.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("firebase", "Notification Message: " +
remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getBody());
}
private void showNotification(String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Firebase Push Notification");
builder.setContentText(msg);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
And now add these two
classes to your manifest.xml file like this:-
<service
android:name=".services.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
android:name=".services.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
You are done with
firebase notification work. Now go to Firebase
Console and select your registered application. Look for a
Notification option in left panel and click.
Compose your
notification message and send. Look this link for more
clearance. Best of Luck :)
Thanks , article is helpful for implementing FCM :)
ReplyDeleteThat's amazing informative post, I want to add one more thing, If you want to make your web visitor to your subscriber then you should definitely check gravitec lifetime deal Best push notification for website ever.
ReplyDelete