Search code examples
androidandroid-activityservicenotificationslockscreen

Start an activity from notification over the secure lockScreen


im stuck trying to show an activity from notification in secure lockscreen (in not secure lockscreen I achieve this).

I follow some questions an answers from StackOverflow but none resolve my problem.

I will post parts of my code.

MainActivity (activity)

This is executed in onCreate()

Intent intentService = new Intent(this,NotificationService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(intentService);

    } else {
        startService(intentService);
    }

NotificationService (service)

This is executed in onStartCommand (tried to in onCreate)

  public void crearNotificacion(){
    RemoteViews rmv = new RemoteViews(getPackageName(),R.layout.notification_custom);

    /*Intent intent = new Intent(this, wPerfil.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(this, 54, intent,PendingIntent.FLAG_UPDATE_CURRENT);*/
    Intent intent = new Intent(this,NotificationIntentService.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getService(this,54,intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    createNotificationChannel();// Este trozo de codigo es para versiones +26
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(rmv)
            .setSmallIcon(R.drawable.ic_trasnparent)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setShowWhen(false)
            .setGroupSummary(false)
            .setAutoCancel(true);

    startForeground(intentID,builder.build());

}

// CODIGO DE LA DOC DE ANDROID para versiones +26
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Notificacion +26";
        String description = "Notificacion para 26+";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);

    }
}

NotificationIntentService (IntentService)

 @Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        handleNotfication();
    }
}


private void handleNotfication(){
    Intent intent = new Intent(this,wPerfil.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

ClassWhatIWantToLoad (activity)

This is executed in the onCreate

  if (Build.VERSION.SDK_INT >= 27) {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
    }else{
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

Permissions gived in Manifest

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />

Hope this parts of code help you to understand my problem. I read in some other questions people achieve this doing the same I do, but i cant. What is wrong? If you have another solution like a widget that I can open in lockScreen tell me please.


Solution

  • Try deleting from the notification creation

    .setContentIntent(pendingIntent)

    And instead add

    rmv.setOnClickPendingIntent(R.id.layoutID, pendingIntent);

    before you set the customview on the notification. R.id.layoutID should be the ID of the relativelayout for your custom notification layout.