Now google has shifted its Google Cloud Messaging to Firebase. So I am making this article on how to add Firebase Push Notifications
in Android Application. Adding Firebase Push Notifications to android
is very easy task Just add two class files and few permissions in
AndroidManifest.xml and few line in Gradle files.
That’s it your Firebase Push Notifications in Android is set to go
Adding Firebase Push Notifications to Android involves two parts.
after adding above line code should like in below Image
compile 'com.google.firebase:firebase-core:9.4.0'
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.androidmkab.firebase.app.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
import com.google.firebase.iid.FirebaseInstanceId;
public class MyFirebaseInstanceIdService extends com.google.firebase.iid.FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token in logcat
Log.e(TAG, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "Android News App";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//It is optional
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
That’s it your Firebase Push Notifications in Android is set to go
Adding Firebase Push Notifications to Android involves two parts.
- Setting up Firebase project in your Firebase Console (Watch Video above to know how to do it)
- Setting up your Android Studio Project (Follow to next step after doing the first step).
- You can continue this procedure to your existing Android Studio project.
- Copy google-services.json to your App folder in your project. (watch Video above to know how to get this file).
- Next, add below line to Project Gradle file
classpath 'com.google.gms:google-services:3.0.0'
after adding above line code should like in below Image
- Now add below dependencies in app Gradle file
compile 'com.google.firebase:firebase-core:9.4.0'
- And also add below line at the end of Gradle file.
- After adding above line code should look below Image
- Android Studio will prompt you with Sync Now sync the project.
- Next, add Internet Permission to your AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
- Now add below services to your Manifest file in between application tags.
<!--fcm-->
<service android:name="com.androidmkab.firebase.app.MyFirebaseMessagingService"><intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.androidmkab.firebase.app.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
In above code change com.androidmkab.firebase.app to your package name. After adding above codes your Android Manifest file should look like below Image.
Now create a new class file with Name MyFirebaseInstanceIdService and add below code to it by replacing everything except the first line which will show your package name.
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
public class MyFirebaseInstanceIdService extends com.google.firebase.iid.FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token in logcat
Log.e(TAG, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
- Now create another class file with name MyFirebaseMessagingService and below code to it same as above.
import android.app.NotificationManager;
import android.app.PendingIntent;import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "Android News App";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//It is optional
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Now run app,
If you get any error about unresolved mimmap, simple create a folder with the name "mipmap" in the layout folder where images folder are in your source code and put the ic_launcher image file in it.
Now run the code in your Android Device or Emulator (Make sure Google
Play Services are supported if you are using the emulator) after the
app is launched on your device or emulator, go to Firebase Console and send notifications Lala.. you successfully sent and received Firebase Push Notifications.
Because of technology now you can create a website quick and simply, in addition to for free. Advertising websites like craigslist are a fantastic method to begin advertising for nearly any organization. You are curious to know more about buy push notification ads, click here.
ReplyDeleteSome will simply give you a list of keywords and tell you how to choose them, but there are some that specialize in the specific niche that you are trying to reach. To know more about hiring ppc agency, check here.
ReplyDelete