Firebase Google Cloud Messaging [ Push Notification ]
Firebase Google Cloud Messaging [ Push Notification ]
For that, 1st of all we need to create a firebase project and connect our app with that ...
2. Goto: Tools -> Firebase
3. Here we can see many options for firebase services. But for now, we need Cloud Messaging. so create a project and connect it.
4. If you create a firebase project by going to firebase console and creating manually then you can select: "Choose an existing firebase or google project".
But here I did not create any project before so I just select "Create a new firebase project" and connect it with my app. [my way is easy :) ]
5. Now we need to add some dependencies for FCM (Firebase Cloud Messaging) service. rather than doing it by manually we can do it as below so easily.
6. It will take some time for adding dependencies and Sync them...
7. Now we are ready for Coding.
--------------------------------------------------------------------------------------------------------------------------
9. Everything has been told here. So NOW DO IT.
10. OK, now we can run/ install this on a virtual/real device and check the LOG/Android Monitor.
And search for the device token and save it in a safe place [token.txt].
So Save the token in any [.txt] file. Cause we will use this token for identifying the specific device and send them notification by Firebase cloud messaging.
12. OK. now we got Token. so now we are trying to focus on when message/notification received our device what should our device do. For that 1st we need to setup firebase cloud messaging service.
we can learn from below picture.
13. Lets do it or Code it:
14. so we add service to manifest ... and we ADD class and write code for handle receive a message.
Our "onMessageReceived" method invokes/calls everytime when device recieves any message from firebase.
so now we are going to create notification when message recieves.
NotE: OH one more thing here we can see into "onMessageReceived" method "remoteMessage" variable got all the data about this message like body, titile.
----------------------------------------------------------------------------------------------
so I created 2 class 1st.
Class-1: Constants.java
Class-2: MyNotificationManager.java
16. Now just we should call the "displayNotification()" method in firebaseMessagingservice class: just like below:-
17. Enough for today guys :
============ push Notification Coding Done ... ..................
goto-> https://console.firebase.google.com
Click your project -> [FCMDemo]
Click to -> cloud messaging
Click to -> send your 1st message
fill up form ...
then Just send you will get your desire "Push Notification "
🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏Project Done 🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
For that, 1st of all we need to create a firebase project and connect our app with that ...
Part-1: Connect Firebase Project
1. Open your Android project in Android Studio.2. Goto: Tools -> Firebase
3. Here we can see many options for firebase services. But for now, we need Cloud Messaging. so create a project and connect it.
4. If you create a firebase project by going to firebase console and creating manually then you can select: "Choose an existing firebase or google project".
But here I did not create any project before so I just select "Create a new firebase project" and connect it with my app. [my way is easy :) ]
5. Now we need to add some dependencies for FCM (Firebase Cloud Messaging) service. rather than doing it by manually we can do it as below so easily.
6. It will take some time for adding dependencies and Sync them...
7. Now we are ready for Coding.
--------------------------------------------------------------------------------------------------------------------------
Part-2: Get Device Token.
8. So for sending a push notification to a specific user, we need his or her Device Token. By which we can identify them. so how to get the device Token.9. Everything has been told here. So NOW DO IT.
10. OK, now we can run/ install this on a virtual/real device and check the LOG/Android Monitor.
And search for the device token and save it in a safe place [token.txt].
11. Note: AS we know that when we 1st-time install any app our device token generated/ registered. after that, if we run again this app, again and again, the device token remains the same. it doesn't regenerate the token until we UNINSTALL the app and Install it again from the scratch ...
So Save the token in any [.txt] file. Cause we will use this token for identifying the specific device and send them notification by Firebase cloud messaging.
Part-3: Set Up Firebase Messaging service.
12. OK. now we got Token. so now we are trying to focus on when message/notification received our device what should our device do. For that 1st we need to setup firebase cloud messaging service.
we can learn from below picture.
13. Lets do it or Code it:
14. so we add service to manifest ... and we ADD class and write code for handle receive a message.
Our "onMessageReceived" method invokes/calls everytime when device recieves any message from firebase.
so now we are going to create notification when message recieves.
NotE: OH one more thing here we can see into "onMessageReceived" method "remoteMessage" variable got all the data about this message like body, titile.
----------------------------------------------------------------------------------------------
Part-4: Build Notification
15. there are many ways to build notification and notify. But I love to do that in an organized way ... ;)so I created 2 class 1st.
Class-1: Constants.java
package com.nirjhor3029.fcmdemo;
/** * Created by nirjhor on 6/3/2018. */
public class Constants {
public static final String Channel_ID = "myChannelID";
public static final String Channel_Name = "myChannelName";
public static final String Channel_des = "myChannelDescription";
}
Class-2: MyNotificationManager.java
package com.nirjhor3029.fcmdemo;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
/** * Created by nirjhor on 6/3/2018. */
public class MyNotificationManager {
private Context mCtx;
private static MyNotificationManager mInstance;
public MyNotificationManager(Context mCtx) {
this.mCtx = mCtx;
}
public static synchronized MyNotificationManager getmInstance(Context context)
{
if(mInstance == null)
{
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title,String body)
{
buildNotificationChannel();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx,Constants.Channel_ID);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
mBuilder.setContentTitle(title);
mBuilder.setContentText(body);
mBuilder.setAutoCancel(true);
Intent intent = new Intent(mCtx,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null)
{
notificationManager.notify(1,mBuilder.build());
}
}
public void buildNotificationChannel()
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel =
new NotificationChannel(Constants.Channel_ID,Constants.Channel_Name,NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(Constants.Channel_des);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});
mNotificationManager.createNotificationChannel(mChannel);
}
}
}
16. Now just we should call the "displayNotification()" method in firebaseMessagingservice class: just like below:-
17. Enough for today guys :
============ push Notification Coding Done ... ..................
Part-5: Send Push Notification
18. [the last thing ]: send notification by the firebase:goto-> https://console.firebase.google.com
Click your project -> [FCMDemo]
Click to -> cloud messaging
Click to -> send your 1st message
fill up form ...
then Just send you will get your desire "Push Notification "
🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏Project Done 🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏



মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন