Header Ads

How to Create Push Notification for Android App in Android Studio

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




1.  So for this, first you have to sign in or create your account in Firebase, you can easily create your Google Account for free. After the account is created, you have to click in Get Started-


2. Then you have to go to Add Project. Then enter the name of your project, then click on Continue. Again you have to click on Continue.


3. select the country if you need, Then click on Android's Icon- after that select the Firebase account and Create a project. Then enter the package name of the app, then you have to enter a nick name of your app, after that click on Register App-


4. Then you have to download the file, then click on Next.


5.  After that you have to go to Android Studio, then select the project by dropdown then Expend package name then right click on the app then click on Show In explorer. then find out App folder and paste a file into the App folder which you downloaded file from the File Firebase before. ones you did close the file explorer




6. Then you have to go to Firebase and copy the code from third step at Add firebase SDK underneath dependencies, after that you have to go to Android Studio, then double click in build.gradle inside Gradle. The code that was copied from Firebase has to be pasted in the build.gradle.



7. Then you have to double click inside the app by going to build.gradle Then paste the code given below (you may have to update the version) -


implementation platform('com.google.firebase:firebase-bom:28.0.1')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-messaging:22.0.0'

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

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


8. Then you have to connect your android studio to Interner and click in Sync Now, and wait a bit till sync is done. Now you have to go to Firebase and click on Next, after that click on Continue in the console-Now you have to create two new java classes, for that we will again go to Android from the project dropdown-After that you have to create a java class named MyFirebaseMessagingService, then click in Enter-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);

}
}


9. 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);
}
}


10. 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>


11. 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.



1 comment:

Powered by Blogger.