package fptu.ninhtbm.demofirebase; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Color; import android.os.Build; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.core.app.RemoteInput; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String CHANNEL_ID = "SECOND_CHANNEL_ID"; private static final String NOTIFICATION_CHANNEL = "SECOND_CHANNEL"; private static final String REMOTE_TEXT_KEY = "REMOTE_TEXT_KEY"; private int count = 0; @RequiresApi(api = Build.VERSION_CODES.O) @Override public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); if (remoteMessage.getData().size() > 0) { sendNotification(remoteMessage.getData().toString()); } } @RequiresApi(api = Build.VERSION_CODES.O) public void sendNotification(String message) { RemoteInput remoteInput = new RemoteInput.Builder(REMOTE_TEXT_KEY).setLabel("nhét chữ vào đây nè").build(); Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action action = new NotificationCompat.Action.Builder(android.R.drawable.ic_dialog_info, "Reply", pendingIntent) .addRemoteInput(remoteInput).build(); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setColor(Color.RED) .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle("Noti đến từ firebase nè") .setContentText(message) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH) .addAction(action); Notification notification = builder.build(); NotificationChannel channel = new NotificationChannel(CHANNEL_ID, NOTIFICATION_CHANNEL, NotificationManager.IMPORTANCE_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); notificationManager.notify(200 + count++, notification); } }