Search code examples
androidandroid-intentnotificationsandroid-pendingintentremoteview

how not to open the app by clicking on the notification?


this is my first time with Notification.

using this code, I can read what is written in the Log():

 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    contentView.setTextViewText(R.id.title, "Custom notification");
    contentView.setTextViewText(R.id.text, "This is a custom layout");

    Intent intentDown = new Intent(this, myIntentClass.class);
    intentDown.putExtra("DO", "down");
    PendingIntent pDown = PendingIntent.getActivity(this,0,intentDown,0);
    contentView.setOnClickPendingIntent(R.id.drop_down, pDown);

    Intent intentUp = new Intent(this, myIntentClass.class);
    intentUp.putExtra("DO", "up");
    PendingIntent pUp = PendingIntent.getActivity(this,1,intentUp,0);
    contentView.setOnClickPendingIntent(R.id.drop_up, pUp);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelSmoke")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContent(contentView)
            .setSubText("Sub Text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setOngoing(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(100, builder.build());

This is my Intent class:

public class myIntentClass extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String action = (String) Objects.requireNonNull(getIntent().getExtras()).get("DO");
    assert action != null;
    if (action.equals("down")) {
        Log.d("CLICKED", "DOWN");
        finish();
    }else if(action.equals("up")){
        Log.d("CLICKED"," UP");
        finish();
    }
}
}

But, when I click on "R.id.drop_down" or "R.id.drop_up" button, the app opens. Is possible to non open app (like netflix notification, using chromecast).

EDIT: I solved using reciever instead of IntentClass.


Solution

  • Just use Reciever instead of IntentClass