Search code examples
androidpush-notificationnotificationscustom-notification

Android notifications custom view: cannot click on the button in the custom view when the app terminated


I assigned a custom view to notification builder and set pending intent for button click in the custom view, but when the app not running, the button can't be clicked!

my implementation:

RemoteViews notificationLayout = new RemoteViews(getPackageName(),R.layout.emotions_notification);

Intent veryDissatisfiedIntent =  new Intent(this, ReceiverService.class);
intent.setAction(EMOTIONS_VERY_DISSATISFIED_ACTION);

PendingIntent pendingIntentVeryDissatisfied = PendingIntent.getService(this, id, veryDissatisfiedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationLayout.setOnClickPendingIntent(R.id.sentiment_very_dissatisfied, pendingIntentVeryDissatisfied);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channel_ID)

  .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  .setSound(uri)
  .setAutoCancel(true);

  mBuilder.setContent(notificationLayout);
  mBuilder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
  mBuilder.setCustomBigContentView(notificationLayout);

 notificationManager.notify(id, mBuilder.build());

Receiver Service:

    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String action = intent.getAction();
        Bundle extras = intent.getExtras();
       int notificationId = extras.getInt("notificationId");
          switch (action) {
            case FirebasePluginMessagingService.EMOTIONS_VERY_DISSATISFIED_ACTION:
              // do something
              break;
}
   return START_STICKY;
  notificationManager.cancel(notificationId);
}

Solution

  • I solved this by:

    startForeground(id, mBuilder.build());