1. Tạo project mới
2. Vào manifest khai báo quyền
<uses-permission android:name="android.permission.INTERNET" />
3. Copy lấy 1 cái ảnh vào drawable để làm biểu tượng
Ảnh bạn tự chọn thôi, chú ý tên file ảnh không dấu, không cách, không viết hoa, không có dấu trừ, bắt đầu là chữ cái không phải số.
4. Trong layout của MainActivity tạo 1 cái textview để nhận diện
android:text="Zezo.dev Demo firebase"
android:textSize="30dp"
android:padding="20dp"
android:textStyle="bold"
5. Tạo layout cho câu thông báo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<!-- Parent Layout of ImageView -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--Image to be displayed beside the notification text-->
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@drawable/demo_android" />
</LinearLayout>
<!-- Parent layout for holding the Title and the Body-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp">
<!-- TextView for Title -->
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#000"
android:textStyle="bold" />
<!-- TextView for Body -->
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
6. Tạo lớp FirebaseMessageReceiver để nhận notify
package vn.edu.spx.demofirebasenotify;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessageReceiver extends FirebaseMessagingService {
// Override onMessageReceived() method to extract the
// title and
// body from the message passed in FCM
@Override
public void
onMessageReceived(RemoteMessage remoteMessage) {
// First case when notifications are received via
// data event
// Here, 'title' and 'message' are the assumed names
// of JSON
// attributes. Since here we do not have any data
// payload, This section is commented out. It is
// here only for reference purposes.
/*if(remoteMessage.getData().size()>0){
showNotification(remoteMessage.getData().get("title"),
remoteMessage.getData().get("message"));
}*/
// Second case when notification payload is
// received.
if (remoteMessage.getNotification() != null) {
// Since the notification is received directly from
// FCM, the title and the body can be fetched
// directly as below.
showNotification(
remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody());
}
}
// Method to get the custom Design for the display of
// notification.
private RemoteViews getCustomDesign(String title,
String message) {
RemoteViews remoteViews = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.layout_notify);
remoteViews.setTextViewText(R.id.title, title);
remoteViews.setTextViewText(R.id.message, message);
remoteViews.setImageViewResource(R.id.icon,
R.drawable.demo_android);
return remoteViews;
}
// Method to display the notifications
public void showNotification(String title,
String message) {
// Pass the intent to switch to the MainActivity
Intent intent
= new Intent(this, MainActivity.class);
// Assign channel ID
String channel_id = "notification_channel";
// Here FLAG_ACTIVITY_CLEAR_TOP flag is set to clear
// the activities present in the activity stack,
// on the top of the Activity that is to be launched
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Pass the intent to PendingIntent to start the
// next Activity
PendingIntent pendingIntent
= PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
// Create a Builder object using NotificationCompat
// class. This will allow control over all the flags
NotificationCompat.Builder builder
= new NotificationCompat
.Builder(getApplicationContext(),
channel_id)
.setSmallIcon(R.drawable.demo_android)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000,
1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
// A customized design for the notification can be
// set only for Android versions 4.1 and above. Thus
// condition for the same is checked here.
if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.JELLY_BEAN) {
builder = builder.setContent(
getCustomDesign(title, message));
} // If Android Version is lower than Jelly Beans,
// customized layout cannot be used and thus the
// layout is set as follows
else {
builder = builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.demo_android);
}
// Create an object of NotificationManager class to
// notify the
// user of events that happen in the background.
NotificationManager notificationManager
= (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
// Check if the Android Version is greater than Oreo
if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel
= new NotificationChannel(
channel_id, "web_app",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(
notificationChannel);
}
notificationManager.notify(0, builder.build());
}
}
7. Khai báo service trong mainifest
<service android:name=".FirebaseMessageReceiver" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
https://console.firebase.google.com/