0

I am implementing firebase push notification in my app, the app is showing the push notification also. But my problem is - I may send different types of push notifications based on different topics.

Ex - 1. If I am sending a push notification on App Update, then on clicking that notification by the user, he should be redirected to PlayStore

  1. If I am sending a push notification on New Contents, then on clicking that notification by the user, he should be redirected to App's MainActivity

  2. If I am sending a push notification on New Contest, then on clicking that notification by the user, he should be redirected to App's ContestActivity

I implemented it using the below code in "MessageReceiver" class as --

public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();

        showNotifications(title, message);
    }
private void showNotifications(String title, String msg) {
        Intent i = null;

        if(title.equals("DoUp Now - Update")){
            Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.s2s.doupnow"); // missing 'http://' will cause crashed
            i = new Intent(Intent.ACTION_VIEW, uri);
        }
        else if(title.equals("DoUp Now - New Content")){
            i = new Intent(this, MainActivity.class);
        }
        else if(title.equals("DoUp Now - New Notification")){
            i = new Intent(this, NotificationActivity.class);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE,
                i, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager mNotifyManager;
        NotificationCompat.Builder mBuilder, mBuilderOld;

        final int NOTIFICATION_ID = 1;
        final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";

        mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            mNotifyManager.createNotificationChannel(notificationChannel);

            mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            mBuilder.setContentTitle(title)
                    .setContentText(msg)
                    .setSmallIcon(R.drawable.doupnowlogo)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent);

            mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
        else
        {
            mBuilderOld = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            mBuilderOld.setContentTitle(title)
                    .setContentText(msg)
                    .setSmallIcon(R.drawable.doupnowlogo)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX);

            mNotifyManager.notify(NOTIFICATION_ID, mBuilderOld.build());
        }
    }

But on clicking any notification by the user, he is always redirected to App's MainActivity only.

Can anyone guide me where i am missing.

DAA
  • 1,346
  • 2
  • 11
  • 19
  • https://stackoverflow.com/questions/7184963/how-to-set-click-listener-for-notification – Ashvin solanki Sep 24 '18 at 10:02
  • From code, It seems their is some mistake in if condition that you have added for different intent. Debug and check that part. Also make sure notification id and Pending intent request code should be different for each notification, else your notification will get override by new one. – Keyur Thumar Sep 24 '18 at 10:04
  • Possible duplicate of [Firebase onMessageReceived not called](https://stackoverflow.com/questions/45671869/firebase-onmessagereceived-not-called) – JacksOnF1re Sep 24 '18 at 13:03

1 Answers1

0
  1. If you are comparing title using api the you have to check the title using JSON object like this.

    JSONObject data = json.getJSONObject("data");
    
    String title = data.getString("title");
    String message = data.getString("message");
    
  2. Make sure the title you get is equal as title you are comparing and add flags in intent.

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
Dharman
  • 30,962
  • 25
  • 85
  • 135
Gaurav sappal
  • 81
  • 1
  • 5