Skip to content Skip to sidebar Skip to footer

Launch Activity When User Taps On A Notification From The Lockscreen

I want to be able to tap on a notification when the device is locked and launch an activity without unlocking the device. I added some flags to the activity in the onCreate() metho

Solution 1:

Try this!!!

private NotificationCompat.Builder mBuilder;

IntentnotifyIntent=newIntent(getApplicationContext(), MainActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntentpendingIntent= PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
    mBuilder = newNotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon())
            .setContent(remoteViews)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true)
            .setOngoing(true);

this is method get notification icon on device 5.0 and lower

privateintgetNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.notification_icon : R.mipmap.ic_launcher;
}

Remove code below in onCreate

Windowwindow = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Post a Comment for "Launch Activity When User Taps On A Notification From The Lockscreen"