2

I want to set onclick on notification to go to specific activity that i getting from backend.(FireBase). At present when i clicking on notification it open application launcher activity.

AL.
  • 36,815
  • 10
  • 142
  • 281
Prerna Yadav
  • 21
  • 1
  • 3

2 Answers2

0

Please check this is MyFirebaseMessagingService file here i'm set an onclick and open a different activity:-

  • Here in first "if (json instanceof JSONObject) {" condition get response from json response got from notification and then open a comment activity.

  • And in "else" got simple message and display on notification and restart app from Splash Screen.

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
            private static final String TAG = "MyFirebaseMsgService";
            Context mContext;
    
            /**
             * Called when message is received.
             *
             * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
             */
            // [START receive_message]
            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {
                // TODO(developer): Handle FCM messages here.
    
                Log.d(TAG, "From: " + remoteMessage.getFrom());
                Log.d(TAG, "Notification Message getMessageId: " + remoteMessage.getMessageId());
                Log.d(TAG, "Notification Message getMessage: " + remoteMessage.getData().get("message"));
                Log.d(TAG, "Notification Message getAction: " + remoteMessage.getData().get("action"));
                Log.d(TAG, "Notification Message ImageURL: " + remoteMessage.getData().get("imageUrl"));
                Log.d(TAG, "Notification Message from: " + remoteMessage.getData().get("from"));
                Log.d(TAG, "Notification Message google.message_id " + remoteMessage.getData().get("google.message_id"));
                mContext = getApplicationContext();
    
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                String msg = remoteMessage.getData().get("message");
                try {
                    sendNotification(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
            private void sendNotification(String message) throws JSONException {
                String commenterFname = "", commenterLname = "", commenterProfileImage = "", userID = "", postID = "";
    
                Object json = new JSONTokener(message).nextValue();
                if (json instanceof JSONObject) {
                    try {
                        JSONObject msg = new JSONObject(message);
                        commenterFname = msg.getString(Constant.JSON_KEY.COM_FNAME);
                        commenterLname = msg.getString(Constant.JSON_KEY.COM_LNAME);
                        commenterProfileImage = msg.getString(Constant.JSON_KEY.COM_IMAGE);
                        userID = msg.getString(Constant.JSON_KEY.USER_ID);
                        postID = msg.getString(Constant.JSON_KEY.POST_ID);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
    
                    String msg = commenterFname + " " + commenterLname + " " + getResources().getString(R.string.commented_on_your_post);
    
                    int id = (int) System.currentTimeMillis();
                    Intent notificationIntent = new Intent(this, CommentActivity.class);
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    notificationIntent.putExtra("postID", postID);
                    notificationIntent.setAction(postID);
                    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                            notificationIntent, 0);
    
                    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                            .setContentTitle("App_name")
                            .setContentText(msg)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                    } else {
                        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                    }
    
                    NotificationManager notificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
                    notificationManager.notify(id, notificationBuilder.build());
    
                } else {
                    int id = (int) System.currentTimeMillis();
                    Intent notificationIntent = new Intent(this, SplashActivity.class);
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                            notificationIntent, 0);
    
                    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                            .setContentTitle("KhetiVadi")
                            .setContentText(message)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                    } else {
                        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                    }
    
                    NotificationManager notificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
                    notificationManager.notify(id, notificationBuilder.build());
                }
            }
        }
    

Hope it's helps you.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
0

if you are sending {"notification":""} data from back end android will show a notification in the notification tray automatically(only if you integrated FCM sdk).

If you want to take control over the notification,don't send notification object from backend,send only data object(if you are sending notification from firebase console for testing,notification object will be there by default)

Now onMessageReceived() method will be executed whenever you send notification without notification object

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String data=remoteMessage.getData();
        //parse data and show notification
        showNotification(parsedMessageData);
    }

    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("showNotification", true);
        intent.putExtra("params", params);
        if (page != null && page.equals(VIEW_INDENT)) {
            intent.putExtra("page", HomeNavigationController.DISPLAY_INDENT_TAG);
        }else if(page != null && page.equals(PAYMENT_SUMMERY)){
            intent.putExtra("page", HomeNavigationController.DISPLAY_PAYMENT_SUMMERY_TAG);
            intent.putExtra("month",paymentParams.getMonth());
            intent.putExtra("year",paymentParams.getYear());
            intent.putExtra("slotId",paymentParams.getSlotId());
        }
        else {
            intent.putExtra("page", HomeNavigationController.PRODUCT_LIST_TAG);
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        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)
                .setSmallIcon(R.drawable.farm_taaza_logo)
                .setContentTitle("Sample Notification")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

Main Activity will be started with the passed arguments as putExtra and from there you can route to the corresponding Activity according to the parameteres.

Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37