0

I receive a notification in MainActivity. When I click on it, it should open the dialog fragment. Currently I am doing this -

String textNotificationMessage = textMessageReceivedEvent.getMessage();

Intent notificationIntent = new Intent(MainActivity.this, MessagingDialogFragment.class);
        notificationIntent.putExtra("NotificationMessage",textNotificationMessage);

        MessagingDialogFragment messagingDialogFragment = (MessagingDialogFragment) MessagingDialogFragment.instantiate(MainActivity.this, MessagingDialogFragment.class.getName());
        messagingDialogFragment.show(getSupportFragmentManager(),MessagingDialogFragment.class.getName());

        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

What this does is, whenever I have a notifictaion, it opens the DialogFragment automatically without a click. But I need it to open after a click. How do I achieve this?

  • when are you executing this code? inside onMessageReceived()? – Aman Grover Sep 12 '16 at 08:44
  • Yes... The notification pop up code is in this method. I want to implement on click of a notification – Maria Anasthesia Sep 12 '16 at 08:45
  • 1
    Possible duplicate of [Show Dialoge using Pending Intent In Android](http://stackoverflow.com/questions/15874144/show-dialoge-using-pending-intent-in-android) – mdtuyen Sep 12 '16 at 08:59
  • @phongvan It is about not showing notification but showing a dialog. My question is to show dialog based on click of notification – Maria Anasthesia Sep 12 '16 at 09:06
  • I know, notification use intent to call an activity. As the recommend I give for you. It use intent to open an activity. The activity not flattern, it open an dialog dirrectly. – mdtuyen Sep 12 '16 at 09:08

2 Answers2

1

Done like this create an activity named MyDialog.java Now in your manifest file do like this given below

<activity 
android:name=". MyDialog"
android:theme="@android:style/Theme.Dialog" />

now navigate to this activity on click event of notification.

Prashant Sharma
  • 1,357
  • 1
  • 21
  • 31
0

The only way to set onClickListener on a notification is through a PendingIntent. Just make the PendingIntent open up one of your Activity and have your Activity be complete transparent and put the code of opening a dialog in onCreate() and finish() the Activity on dismiss of the dialog.

Intent notifyIntent = new Intent(context,ActivityContainingDialog.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID, 
            notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Aman Grover
  • 1,621
  • 1
  • 21
  • 41