Show Dialog when touch notification on Android

1.9k views Asked by At

I already built push notification by using Firebase Cloud Messaging on Eclipse Project. Now I want to make dialog with OK button when I touch the notification on status bar.

Can anyone help me? or suggest how to handle it? FYI, anytime (when the apps on background or in foreground) if I touch the notification on top it will show a dialog box.

Many thanks.

1

There are 1 answers

6
Harjot Singh Oberai On BEST ANSWER

Firstly you need to use a pending intent with your notification that defines how to handle the notification click.

    Intent notificationIntent = new Intent(this, DialogActivity.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Notification notification = new Notification.Builder(this)
             // YOUR Notification parameters
            .build();

    notification.contentIntent = pendingNotificationIntent;

See that the intent points to a DialogActivity, so we need to create a DialogActivity to handle the intent. See the code below :

public class DailogActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Get Extra data from intent if you pass something
    // Intent intent = getIntent();
    // Bundle extras = intent.getExtras();

    // Show the popup dialog
    showNewDialog(0);
}

public void showNewDialog(int id) {
    // TODO : Code to show the new dialog
}
}