Skip to content Skip to sidebar Skip to footer

Not Opening The New Activity After Clicking On The Notification Android

Here i want to open a new activity class called View. Once after click on the generated notification. How can i do that?? Some immediate reply will be appreciated. It worked well e

Solution 1:

Use below code:

inticon= R.drawable.app_notification_icon;
        longwhen= System.currentTimeMillis();

        NotificationManagernotificationManager= (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notificationnotification=newNotification(icon, message, when);

        Stringtitle= context.getString(R.string.app_name);

        IntentnotificationIntent=newIntent(context,
                NotificationDialog.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra(IntentConstantsUtils.MESSAGE, message);
        PendingIntentintent= PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

Create a new java class:

publicclassNotificationDialogextendsActivity{

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String message = getIntent().getStringExtra(IntentConstantsUtils.MESSAGE);
            showDialog(this, message, newDialogInterface.OnClickListener() {
                @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
        }

privatevoidshowDialog(Context context, String message,
            DialogInterface.OnClickListener onOkClick) {
        AlertDialog alertDialog = newAlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle("Alert");

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting Icon to Dialog// alertDialog.setIcon(R.drawable.tick);// Setting OK Button
        alertDialog.setButton("OK", onOkClick);

        // Showing Alert Message
        alertDialog.show();
    }
    }

In manifest add

<activityandroid:name="com.shufflecloud.smg.activities.NotificationDialog"android:screenOrientation="portrait"android:theme="@android:style/Theme.Dialog" ></activity>

Solution 2:

Change

    Intent notificationIntent = newIntent(context, View.class);

to

    Intent notificationIntent = newIntent(context, View.class, Intent.FLAG_ACTIVITY_NEW_TASK);

Solution 3:

This is issue in Kitkat 4.4 try with following line to you activity which you want to open on click of notification

android:exported="true"

and add flag to notification inttent

Notification.FLAG_AUTO_CANCEL;

Post a Comment for "Not Opening The New Activity After Clicking On The Notification Android"