Create a SimpleAndroid App with WebView and Push Notification

If you want to send Push Notification from an Android WebView application, so for this you can use Google Cloud Messaging Product of Firebase.

So for this, first you have to sign in or create your account in Firebase, you can easily create your Google Account for free.

1. After the account is created, you have to click in Get Started-

2. Then you have to go to Add Project-

3. Then enter the name of your project, then click on Continue.

4. Again you have to click on Continue –

5. Then it is possible that you will have the option to select the country, after that you will get the option to select the Firebase account, select it and click in the Create project.

6. Then you have to click on Android’s Icon-

7. Then you have to enter the package name of the app, you can see the package name from Android Studio, then you have to enter a nick name of your app, after that you have to click in the Register App-

8. Then you have to download the file from the option shown below, then click on Next.

9. After that you have to go to Android Studio, then select the project from the dropdown.

10. Then you have to open your package name, after that move the mouse in the app and right click, then go to the Show In explorer.

11. Then you have to go inside the App folder and paste those downloaded from the File Firebase into the App folder –

12. Now you can close the file explorer

13. Then you have to go to Firebase and copy the code shown below, after that you have to go to Android Studio, then double click in build.gradle inside Gradle as shown in the image below-

14. The code that was copied from Firebase has to be pasted in the build.gradle at the location shown in the image-

15. Then you have to double click inside the app by going to build.gradle as shown in the image below-

16. Then you have to paste the code given below as shown in the image (you may have to update the version given in the code, for this watch the video above) –

implementation platform('com.google.firebase:firebase-bom:26.1.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-messaging:21.0.0'

Paste the below given code with fire at the place shown in the image under this code-

apply plugin: 'com.google.gms.google-services'

Then you have to connect your android studio to Interner and click in Sync Now, and wait a bit till sync is done.

17. Now you have to go to Firebase and click on Next, after that click on Continue in the console-

18. Now you have to create two new java classes, for that we will again go to Android from the project dropdown-

19. Then you have to go to app> java> domain in which your MainActivity.java is, by going to that folder you have to right click, then going to New and clicking in java class-

After that you have to create a java class named MyFirebaseMessagingService, then click in Enter-

20. After that paste the given code in the place shown in the image and if there comes an error to import the class, then import the class-

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("FCM NOTIFICATION");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());

//Change URL section
String url = remoteMessage.getData().get("url"); //url is the key to be used in firebase for data
intent.putExtra("url",url);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);

}
}

21. Now you have to create another Java Class again as before but this time with the name MyFireBaseInstanceIDService –

Then paste the code given below in the MyFireBaseInstanceIDService class to the location shown in the image-

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService {

private static final String REG_TOKEN = "REG_TOKEN";
@Override
public void onTokenRefresh() {
String recent_token = FirebaseInstanceId.getInstance().getToken();

Log.d(REG_TOKEN,recent_token);
}
}

22. After that you have to go to Android Manifest (app> manifests> AndroidManifest.xml) and paste the below given code as shown in the image-

<service android:name=".MyFireBaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"></action>
</intent-filter>
</service>

23. Now you have to go to MainActivity.java (app> java> domain> MainActivity.java> MainActivity) and paste the code below as shown in the image-

FirebaseMessaging.getInstance().subscribeToTopic("notifications");

Now, before running your application, if you must have been in your application, then uninstall it, then run the app, notification will start in it.

I have told you in the video how to send the notification

Leave a Comment