I have implemented push notification using fcm, but I want the notification to be similar like Flipkart, If I post some any offers and deals to the user, the user must redirect to that page if he click on the push notification. How can I implement that?
So first of all you need an unique tag eg. notification_type or type etc inside the payload that is received in notification this type used to direct the PendingIntent to where it should be redirected.So assuming that you are familiar with FirebaseMessagingService you are managing the notification inside the class which extends FirebaseMessagingService
now inside that class define the object of PendingIntent
here is the step
private PendingIntent pendingIntent = null;
Handle notification like this
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See showChatNotification method below.
//Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// Log.d(TAG, "From: " + remoteMessage.getFrom());
//Logger.d(TAG + " getData: " + remoteMessage.getData().toString());
//Logger.d(TAG + " getData: " + remoteMessage.getNotification().getBody());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
setNotification(data);
} else {
Log.e(TAG, "Notificaiton");
}
}
Here i created method setNotification(data);
to perfrom event on notification received
private void showOtherNotification(Map<String, String> data) {
Date now = new Date();
uniqueId = now.getTime();
if (data.containsKey("type")) {
switch (data.get("type")) {
case "today_diss":
navigetToTodaysDisFragment(data);
break;
case "questionnaire":
navigetToQuestionaire(data);
break;
default:
navigateToHome();
break;
}
} else {
navigateToHome();
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder;
//Large Icon
Bitmap remote_picture = null;
try {
if (data.containsKey("image")) {
if (!data.get("image").isEmpty()) {
remote_picture = BitmapFactory.decodeStream(
(InputStream) new URL(data.get("image")).getContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
// this is a my insertion looking for a solution
int icon = getNotificationIcon();
if (data.containsKey("image") && remote_picture != null) {
notificationBuilder = new NotificationCompat.Builder(this,"default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(StringEscapeUtils.unescapeJava(data.get("message"))))
.setContentText(StringEscapeUtils.unescapeJava(data.get("message")))
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setLargeIcon(remote_picture)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(remote_picture)
.setBigContentTitle(getString(R.string.app_name))
.setSummaryText(StringEscapeUtils.unescapeJava(data.get("message"))));
} else {
notificationBuilder = new NotificationCompat.Builder(this,"default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(StringEscapeUtils.unescapeJava(data.get("message"))))
.setContentText(StringEscapeUtils.unescapeJava(data.get("message")))
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) uniqueId, notificationBuilder.build());
}
Here i also provide you the solution to manage rich notification if your notification contains image you can display on the notification bar
Now if you can see there is peace of code
case "today_diss":
navigetToTodaysDisFragment(data);
break;
case "questionnaire":
navigetToQuestionaire(data);
break;
default:
navigateToHome();
break;
This is the main solution you need this methods will redirect you too the screen where you want to redirect so here I am giving you the example of navigateToHome();
This method will redirect you to the HomeScreen
private void navigateToHome() {
Intent intentToEventPage = new Intent(MyFirebaseMessagingService.this, DashActivity.class);
intentToEventPage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentToEventPage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
pendingIntent = PendingIntent.getActivity(this, (int) uniqueId /* Request code */, intentToEventPage, PendingIntent.FLAG_ONE_SHOT);
}
such way you can modify the pendingIntent
as per your requirement to redirect to the desired activity or view