Open Activity on Notification Click - Intent flag issue











up vote
0
down vote

favorite












I am creating an Android app. I have set up the notification system to open a specific activity/class on notification click. When my App is totally closed and I send a notification and when I click on it opens the correct activity like this example:



App is not running > Notification comes in > Click on the notification > Activity_T opens.



BUT, when my app is running and I get a notification and when I click on it to open the Activity_T it doesn't it loads MainActivity instead.



The flags I use are the follow



    Intent intent = new Intent(mContext, (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("tt", title);
intent.putExtra("bd", body);
mContext.startActivity(intent);


I want to open Activity_T on notification click either the app is running or not. How to force open Activity_T whenever I click on the notification.










share|improve this question


















  • 1




    Can you post the manifest? or some activity tag code
    – jitesh mohite
    Nov 8 at 5:13










  • @jiteshmohite if you are referring to know the launch mode of the Activity_T it is android:launchMode="singleTask" and for Mainactivity there is no launchmode set
    – LoveDroid
    Nov 8 at 5:23










  • try this, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
    – jitesh mohite
    Nov 8 at 5:23










  • @jiteshmohite thank you, I have just tried it but didn't solve the issue.
    – LoveDroid
    Nov 8 at 5:28










  • Remove singleTask from menifest and see.
    – jitesh mohite
    Nov 8 at 5:30















up vote
0
down vote

favorite












I am creating an Android app. I have set up the notification system to open a specific activity/class on notification click. When my App is totally closed and I send a notification and when I click on it opens the correct activity like this example:



App is not running > Notification comes in > Click on the notification > Activity_T opens.



BUT, when my app is running and I get a notification and when I click on it to open the Activity_T it doesn't it loads MainActivity instead.



The flags I use are the follow



    Intent intent = new Intent(mContext, (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("tt", title);
intent.putExtra("bd", body);
mContext.startActivity(intent);


I want to open Activity_T on notification click either the app is running or not. How to force open Activity_T whenever I click on the notification.










share|improve this question


















  • 1




    Can you post the manifest? or some activity tag code
    – jitesh mohite
    Nov 8 at 5:13










  • @jiteshmohite if you are referring to know the launch mode of the Activity_T it is android:launchMode="singleTask" and for Mainactivity there is no launchmode set
    – LoveDroid
    Nov 8 at 5:23










  • try this, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
    – jitesh mohite
    Nov 8 at 5:23










  • @jiteshmohite thank you, I have just tried it but didn't solve the issue.
    – LoveDroid
    Nov 8 at 5:28










  • Remove singleTask from menifest and see.
    – jitesh mohite
    Nov 8 at 5:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am creating an Android app. I have set up the notification system to open a specific activity/class on notification click. When my App is totally closed and I send a notification and when I click on it opens the correct activity like this example:



App is not running > Notification comes in > Click on the notification > Activity_T opens.



BUT, when my app is running and I get a notification and when I click on it to open the Activity_T it doesn't it loads MainActivity instead.



The flags I use are the follow



    Intent intent = new Intent(mContext, (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("tt", title);
intent.putExtra("bd", body);
mContext.startActivity(intent);


I want to open Activity_T on notification click either the app is running or not. How to force open Activity_T whenever I click on the notification.










share|improve this question













I am creating an Android app. I have set up the notification system to open a specific activity/class on notification click. When my App is totally closed and I send a notification and when I click on it opens the correct activity like this example:



App is not running > Notification comes in > Click on the notification > Activity_T opens.



BUT, when my app is running and I get a notification and when I click on it to open the Activity_T it doesn't it loads MainActivity instead.



The flags I use are the follow



    Intent intent = new Intent(mContext, (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("tt", title);
intent.putExtra("bd", body);
mContext.startActivity(intent);


I want to open Activity_T on notification click either the app is running or not. How to force open Activity_T whenever I click on the notification.







android android-intent flags






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 4:32









LoveDroid

257




257








  • 1




    Can you post the manifest? or some activity tag code
    – jitesh mohite
    Nov 8 at 5:13










  • @jiteshmohite if you are referring to know the launch mode of the Activity_T it is android:launchMode="singleTask" and for Mainactivity there is no launchmode set
    – LoveDroid
    Nov 8 at 5:23










  • try this, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
    – jitesh mohite
    Nov 8 at 5:23










  • @jiteshmohite thank you, I have just tried it but didn't solve the issue.
    – LoveDroid
    Nov 8 at 5:28










  • Remove singleTask from menifest and see.
    – jitesh mohite
    Nov 8 at 5:30














  • 1




    Can you post the manifest? or some activity tag code
    – jitesh mohite
    Nov 8 at 5:13










  • @jiteshmohite if you are referring to know the launch mode of the Activity_T it is android:launchMode="singleTask" and for Mainactivity there is no launchmode set
    – LoveDroid
    Nov 8 at 5:23










  • try this, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
    – jitesh mohite
    Nov 8 at 5:23










  • @jiteshmohite thank you, I have just tried it but didn't solve the issue.
    – LoveDroid
    Nov 8 at 5:28










  • Remove singleTask from menifest and see.
    – jitesh mohite
    Nov 8 at 5:30








1




1




Can you post the manifest? or some activity tag code
– jitesh mohite
Nov 8 at 5:13




Can you post the manifest? or some activity tag code
– jitesh mohite
Nov 8 at 5:13












@jiteshmohite if you are referring to know the launch mode of the Activity_T it is android:launchMode="singleTask" and for Mainactivity there is no launchmode set
– LoveDroid
Nov 8 at 5:23




@jiteshmohite if you are referring to know the launch mode of the Activity_T it is android:launchMode="singleTask" and for Mainactivity there is no launchmode set
– LoveDroid
Nov 8 at 5:23












try this, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
– jitesh mohite
Nov 8 at 5:23




try this, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
– jitesh mohite
Nov 8 at 5:23












@jiteshmohite thank you, I have just tried it but didn't solve the issue.
– LoveDroid
Nov 8 at 5:28




@jiteshmohite thank you, I have just tried it but didn't solve the issue.
– LoveDroid
Nov 8 at 5:28












Remove singleTask from menifest and see.
– jitesh mohite
Nov 8 at 5:30




Remove singleTask from menifest and see.
– jitesh mohite
Nov 8 at 5:30












2 Answers
2






active

oldest

votes

















up vote
0
down vote













I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that



Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, "")
.setSmallIcon(R.drawable.my_ic)
.setContentTitle(title)
.setContentText(msg)
.setContentIntent(contentIntent);





share|improve this answer




























    up vote
    0
    down vote













    Try below code...Change YOUR_ACTIVITY.CLASS with your activity name...



    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent=new Intent(this,YOUR_ACTIVITY.CLASS);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("FCM NOTIFICATION");
    notificationBuilder.setContentText(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert notificationManager != null;
    notificationManager.notify(0,notificationBuilder.build());
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201593%2fopen-activity-on-notification-click-intent-flag-issue%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that



      Intent notificationIntent = new Intent(context, MainActivity.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

      NotificationCompat.Builder mBuilder =
      new NotificationCompat.Builder(context, "")
      .setSmallIcon(R.drawable.my_ic)
      .setContentTitle(title)
      .setContentText(msg)
      .setContentIntent(contentIntent);





      share|improve this answer

























        up vote
        0
        down vote













        I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that



        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context, "")
        .setSmallIcon(R.drawable.my_ic)
        .setContentTitle(title)
        .setContentText(msg)
        .setContentIntent(contentIntent);





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that



          Intent notificationIntent = new Intent(context, MainActivity.class);
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

          NotificationCompat.Builder mBuilder =
          new NotificationCompat.Builder(context, "")
          .setSmallIcon(R.drawable.my_ic)
          .setContentTitle(title)
          .setContentText(msg)
          .setContentIntent(contentIntent);





          share|improve this answer












          I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that



          Intent notificationIntent = new Intent(context, MainActivity.class);
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

          NotificationCompat.Builder mBuilder =
          new NotificationCompat.Builder(context, "")
          .setSmallIcon(R.drawable.my_ic)
          .setContentTitle(title)
          .setContentText(msg)
          .setContentIntent(contentIntent);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 5:32









          enesgonez

          666




          666
























              up vote
              0
              down vote













              Try below code...Change YOUR_ACTIVITY.CLASS with your activity name...



              @RequiresApi(api = Build.VERSION_CODES.KITKAT)
              @Override
              public void onMessageReceived(RemoteMessage remoteMessage) {
              Intent intent=new Intent(this,YOUR_ACTIVITY.CLASS);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
              NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
              notificationBuilder.setContentTitle("FCM NOTIFICATION");
              notificationBuilder.setContentText(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
              notificationBuilder.setAutoCancel(true);
              notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
              notificationBuilder.setContentIntent(pendingIntent);
              NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              assert notificationManager != null;
              notificationManager.notify(0,notificationBuilder.build());
              }





              share|improve this answer

























                up vote
                0
                down vote













                Try below code...Change YOUR_ACTIVITY.CLASS with your activity name...



                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void onMessageReceived(RemoteMessage remoteMessage) {
                Intent intent=new Intent(this,YOUR_ACTIVITY.CLASS);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
                NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
                notificationBuilder.setContentTitle("FCM NOTIFICATION");
                notificationBuilder.setContentText(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
                notificationBuilder.setAutoCancel(true);
                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                notificationBuilder.setContentIntent(pendingIntent);
                NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                assert notificationManager != null;
                notificationManager.notify(0,notificationBuilder.build());
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Try below code...Change YOUR_ACTIVITY.CLASS with your activity name...



                  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                  @Override
                  public void onMessageReceived(RemoteMessage remoteMessage) {
                  Intent intent=new Intent(this,YOUR_ACTIVITY.CLASS);
                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
                  NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
                  notificationBuilder.setContentTitle("FCM NOTIFICATION");
                  notificationBuilder.setContentText(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
                  notificationBuilder.setAutoCancel(true);
                  notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                  notificationBuilder.setContentIntent(pendingIntent);
                  NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                  assert notificationManager != null;
                  notificationManager.notify(0,notificationBuilder.build());
                  }





                  share|improve this answer












                  Try below code...Change YOUR_ACTIVITY.CLASS with your activity name...



                  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                  @Override
                  public void onMessageReceived(RemoteMessage remoteMessage) {
                  Intent intent=new Intent(this,YOUR_ACTIVITY.CLASS);
                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
                  NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
                  notificationBuilder.setContentTitle("FCM NOTIFICATION");
                  notificationBuilder.setContentText(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
                  notificationBuilder.setAutoCancel(true);
                  notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                  notificationBuilder.setContentIntent(pendingIntent);
                  NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                  assert notificationManager != null;
                  notificationManager.notify(0,notificationBuilder.build());
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 5:49









                  VikaS

                  3110




                  3110






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201593%2fopen-activity-on-notification-click-intent-flag-issue%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini