Open specific activity when notification was pressed in FCM

I am working on an application in which I need to show a notification. For notification, I use FireBase Cloud Messaging (FCM) . I can get a notification when the application is in the background.

But when I click on the notification, it redirects to the home.java page. I want it to be redirected to the Notification.java page.

So, please tell me how to indicate Activity in on Click of notification. I use two services:

1.) MyFirebaseMessagingService

2.) MyFirebaseInstanceIDService

This is my sample code for the onMessageReceived () method in the MyFirebaseMessagingService class.

public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebaseMessageService"; Bitmap bitmap; public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } // 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 sendNotification method below. } /** * Create and show a simple notification containing the received FCM message. */ private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) { Intent intent = new Intent(this, Notification.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("Notification", TrueOrFalse); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setLargeIcon(image)/*Notification icon image*/ .setContentTitle(messageBody) .setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(image))/*Notification with Image*/ .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } /* *To get a Bitmap image from the URL received * */ public Bitmap getBitmapfromUrl(String imageUrl) { try { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(input); return bitmap; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } 
+17
source share
4 answers

With FCM, you can send two types of messages to clients:

1. Notification messages: sometimes considered a “message display”.

FCM automatically displays a message to end-user devices on behalf of the client application. Notification messages have a predefined set of keys visible to the user.

2. Data messages: that are processed by the client application.

The client application is responsible for processing data messages. Data messages have only user key-value pairs.

According to FCM Document Receive Messages in Android App

  • Notifications are delivered when your application is in the background. In this case, the notification is delivered to the system tray of devices. When a user clicks on a notification, the application launch bar opens by default.
  • Messages with notification and data payload, both background and front. In this case, the notification is delivered to
    The system tray of devices, and data is downloaded in additions about the intentions of your Activity launcher.

Set click_action in the notification:

So, if you want to process messages that arrived in the background, you must send click_action with the message.

click_action is a notification payload parameter

If you want to open your application and perform a specific action, set click_action in the notification payload and map it to the intent filter in the action you want to run.

For example, set click_action to OPEN_ACTIVITY_1 to activate the intent filter, as shown below:

 <intent-filter> <action android:name="OPEN_ACTIVITY_1" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 

The FCM payload is as follows:

 { "to":"some_device_token", "content_available": true, "notification": { "title": "hello", "body": "test message", "click_action": "OPEN_ACTIVITY_1" }, "data": { "extra":"juice" } } 
+31
source

When the application is in the background, Intent should be delivered to your startup activity. So this opens up your Launcher activity. Now you check to see if there is data in the Intent in the start activity, then start the required activity.

+7
source

AndroidManifest.xml

 <activity android:name="YOUR_ACTIVITY"> <intent-filter> <action android:name="com.example.yourapplication_YOUR_NOTIFICATION_NAME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

Your FirebaseMessagingService.java file has the onMessageReceived method:

 public void onMessageReceived(RemoteMessage remoteMessage){ String title=remoteMessage.getNotification().getTitle(); String message=remoteMessage.getNotification().getBody(); String click_action=remoteMessage.getNotification().getClickAction(); Intent intent=new Intent(click_action); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(message); notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); notificationBuilder.setAutoCancel(true); notificationBuilder.setContentIntent(pendingIntent); NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0,notificationBuilder.build()); } 

Your cloud function / server code for notification:

  notification: { title: "TITLE OF NOTIFICATION", body: "NOTIFICATION MESSAGE", sound: "default", click_action: "com.example.myapplication_YOUR_NOTIFICATION_NAME" } 
+3
source

Open the file MyFirebaseMessagingService.java file

inside this file there is a sendNotification () method, in which you must indicate your activity, which you need to go to Intent, as shown below

 Intent intent = new Intent(this, YourActivityName.class); 

if you send several notifications and want to proceed to different actions by clicking on a specific notification, you can use any conditional statement to achieve it, I suggest using a switch as shown below

 private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) { Intent intent = new Intent(); switch(condition) { case '1': intent = new Intent(this, ActivityOne.class); break; case '2': intent = new Intent(this, ActivityTwo.class); break; case '3': intent = new Intent(this, ActivityThree.class); break; default : intent = new Intent(this, DefaultActivity.class); break; } intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("Notification", TrueOrFalse); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); } 

Using this logic, you can open a specific action by clicking Notification in FCM. This works great for me. thanks

+1
source

Source: https://habr.com/ru/post/1012522/


All Articles