This article demonstrates how to add push notification authentication on Android applications using Android Studio.
FCM is a cross-platform messaging solution that lets you reliably deliver the messages at no cost. Firebase is only for mobile platforms and allows you to quickly develop a quality application.
- Notification Message or Display Message.
- Data Message handles the client app.
Notification Message
FCM automatically sends the message to the end-user. Use the Admin SDK or FCM Server protocols and set the notification key.
Data Message
The client app is responsible for processing data messages. Data messages only have custom key-value pairs. Use the Admin SDK or FCM Server protocols and set the data key only.
Step 1
Create a new project in Android Studio from File >> Project and fill in all the necessary details.
Step 2
Enter your application package name and (Nick Name and Debug signing certificate SHA-1) Optional.
You will download a Google-services.json file that just moves the downloaded google-services file into an android app.
Step 3
Next, go to Gradle Scripts >> build.gradle (Module: app).Select build.gradle, The app Gradle compiles the code, and then build types will appear. Just replace that with the following code. Add the Firebase Core and Firebase Message Gradle in your project or you can also use Gradle.
Dependencies Class
- classpath 'com.google.gms:google-services:3.2.0'
Gradle for App
- dependencies {
-
- compile 'com.google.firebase:firebase-core:12.0.0'
- compile 'com.google.firebase:firebase-messaging:12.0.0'
- }
- apply plugin: 'com.google.gms.google-services'
Step 4
Next, go to app >> Java >> package name >> New Java Class. Select the Java Class. Just use the following code.
MyFirebaseMessageService.Java Code
- package io.github.selvaraju_saravanan.notification;
-
- 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.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
-
-
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
-
- private static final String TAG = "FirebaseMsgService";
-
- @Override
- public void onMessageReceived(RemoteMessage remotemsg) {
-
- Log.d(TAG, "From -> " + remotemsg.getFrom());
- Log.d(TAG, "Demo Notification Body -> " + remotemsg.getNotification().getBody());
- sendNotification(remotemsg.getNotification().getBody());
- }
-
-
- private void sendNotification(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 soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("C# Corner")
- .setContentText(messageBody)
- .setAutoCancel(true)
- .setSound(soundUri)
- .setContentIntent(pendingIntent);
-
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0, notificationBuilder.build());
- }
- }
Again, create a new Java class.
Just replace the following change -- but this is my package name, and you can change your application package name.
MyFirebaseInstanceIDService.java code
- package io.github.selvaraju_saravanan.notification;
-
- 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 TAG = "FirebaseIIDServiceDemo";
- public String[] name;
-
- @Override
- public void onTokenRefresh() {
- String token = FirebaseInstanceId.getInstance().getToken();
- System.out.println("my firebase token " + token );
- }
- private void sendRegistrationToServer(String token) {
-
- }
- }
Step 5
After that again go back to
Console Firebase > Click OverView > Notification. Select Notifications and Click Get Started button.
I have already sent one message to my app.
Write the information to the textbox and select your application package name and click send message button.
Review your message and user segment -- you have successfully connected your app.
Step 6
Next, go to Android Studio and deploy the application. Select Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
Output
Deliver the Message that you have deployed the application.
Summary
We have successfully created a push message application. Later we will discuss more cloud apps.