SEND YOUR FIRST ANDROID NOTIFICATION USING
FCM(Firebase Cloud Messaging)
Abstract: Here I will be discussing about how we can send push notification messages to all users (of an Android app) using Firebase Cloud Messaging (FCM)
1. Create an android project in Android Studio
2. Sign in to firebase with google credentials
3. Now go to the firebase console
(i). Here, create your own firebase project
Follow the subsequent steps. Then add your
android app to the firebase clicking the symbol (for android).
(ii). link and register your android app to the
project
4. In this process download the google-services.json
config file. Put the file in you application’s root directory(just inside your
android project’s app folder)
5. Add the firebase android SDK in the project
level and app level build.gradle file:
build.gradle(project leve):
Add the following line into repositories,
dependencies and allprojects section.
// Top-level build file where you can add
configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
}
dependencies {
classpath
'com.google.gms:google-services:4.3.8'
}
}
allprojects {
repositories {
google()
}
}
6. Now modify the build.gradle file(app level):
Add the following lines
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation
platform('com.google.firebase:firebase-bom:26.6.0')
implementation
'com.google.firebase:firebase-analytics'
implementation
'com.google.firebase:firebase-messaging:17.3.4'
}
Remember to choose right firebase-bom library 'com.google.firebase:firebase-bom:26.6.0', otherwise
build process may fail.
The above bom library will work
well with Android sdk 21
7. Now sync the build.gradle file in Android
Studio.
8. Make a subclass of FirebaseMessagingService
->
here override the method onMessageReceived(RemoteMessage remoteMessage)
->
get the title and body of the notification message from this RemoteMessage
object. With remoteMessage.getTitle() get the title, with
remoteMessage.getBody() get the notification message.
Below is the code:
package your.package.name;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessageReceiver extends
FirebaseMessagingService {
@Override
public void
onMessageReceived(RemoteMessage remoteMessage){
if(remoteMessage.getNotification() != null){
String title =
remoteMessage.getNotification().getTitle();
String body =
remoteMessage.getNotification().getBody();
addNotification(body);
}
}
@Override
public void
onNewToken(String token){
}
private void
addNotification(String msg){
//String channel_i =
"AmarSchool Notification";
NotificationCompat.Builder builder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("Test Notification")
.setContentText(msg)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setChannelId(AdmActivity.CHANNEL_ID);
Intent
notificationIntent = new Intent(this, MyActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("message",msg);
PendingIntent
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,
builder.build());
}
}
9. Implement the Notification utilities in this
subclass.
10. Keep your MainActivity unchanged.
11. In the app manifest add the following code
inside the application tag:
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name”
android:theme="@style/AppTheme">
….. ……. ….. ….. …….
<service android:name=".FirebaseMessageReceiver">
<intent-filter>
<action
android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
12. Now build the project and install it in your device.
13. Now again go to Firebase console. open your
project. Go to cloud messaging.
-> [We will go to project settings and
collect the service accounts credentials when we will use our own server to put
logic to the message sent to our clients(users of the said android app)]
14. Click on ‘Send your first message’.
(i)In the window following set the title and
notification text
(ii) Now set the target. Choose your
app(package name) as user segment
(iii) Click review, then publish
Your message will appear to your handset.
No comments:
Post a Comment