0

I'm trying to add 3 buttons to my foreground service notification, but all guides i followed failed

this answer says to add action: https://stackoverflow.com/a/49539463/5679560
this tutorial says to use remoteview https://developer.android.com/training/notify-user/custom-notification

I've tried both options but my notification keeps the default look, even the text doesn't change at all, my notification refers to the foreground service notification is this the problem? isnt possible to add a small button in the foreground service notification?

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this))
        startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + this.getPackageName())).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

    else if (intent.hasExtra(com.tomatedigital.androidutils.Constants.Intent.WINDOW_SERVICE_LAYOUT)) {
        new FloatingWindow(this, LayoutInflater.from(this).inflate(intent.getIntExtra(Constants.Intent.WINDOW_SERVICE_LAYOUT, 0), null), "casa");
        startForeground(1, Notification.startFloatingWindow(this));
    }

    return START_REDELIVER_INTENT;
}

  public static android.app.Notification startFloatingWindow(@NonNull final Context context) {


        @SuppressLint("RemoteViewLayout") RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_floating_window);

        //HelperActivity will be shown at step 4
/*
        Intent stop = new Intent(context, MeuServico.class);
        stop.putExtra("stop", "do");//if necessary

        PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
        //R.id.radio is a button from the layout which is created at step 2  view.setOnClickPendingIntent(R.id.radio, pRadio);

        //Follows exactly my code!
        Intent volume = new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
        volume.putExtra("DO", "volume");</p >

                //HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
                PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
        view.setOnClickPendingIntent(R.id.volume, pVolume);
*/
        return new NotificationCompat.Builder(context, Constants.Notification.FLOATING_WINDOW_NID)
                //       .setSmallIcon(R.drawable.ic_notification_logo)
                .setContentTitle("getString(R.string.foregroundNotification)")
                .setContentText("text")
                //.setCustomContentView(contentView)
                .addAction(R.drawable.ic_stop, "teste", null)
                .setPriority(NotificationCompat.PRIORITY_LOW).build();
    }
}

A little more context to the problem. I'm trying to create a floating window and have a service controlling it. Everything runs fine, the service starts -> goes foreground -> creates the floating window which shows fine even when app is background

but i want to add a CLOSE button in the notification so user can close the floating window...

Btw the text in the floating window says "My app is running tap here to see more info" which definitely isn't what i wrote for the notification

enter image description here

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105

1 Answers1

0

Show a notification like this :

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL);
  builder.setContentTitle("Alarm for: " + (is24 ? _24H : _12H) + (isSnoozeAction ? " (Snoozed)" : ""))
         .setContentText(alarmLabel)
         .setSmallIcon(R.drawable.ic_n_alarm)
         .setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
         .setContentIntent(pi)
         .addAction(new NotificationCompat.Action(R.drawable.ic_n_snooze, "Snooze", actionSnooze))
         .addAction(new NotificationCompat.Action(R.drawable.ic_n_cancel, "Dismiss", actionDismiss));

  manager.notify(NOTIFICATION_ID, builder.build());

addAction adds a button to the notification, and the third argument passed is a PendingIntent, which represents the actual action.

    Intent receiverSnooze = new Intent(context, YourReceiver.class);
    
    receiverSnooze.putExtra("action", "myAction");
       
 
    Intent receiverDismiss = new Intent(context, YourReceiver.class);
    
    receiverDismiss.putExtra("action", "myAction");

   
PendingIntent actionDismiss = PendingIntent.getBroadcast(context, 113, receiverDismiss,
                                                                       PendingIntent.FLAG_ONE_SHOT);
    
PendingIntent actionSnooze = PendingIntent.getBroadcast(context, 114, receiverSnooze,
                                                                      PendingIntent.FLAG_ONE_SHOT);
Noah
  • 567
  • 5
  • 21
  • your answer is wrong, the icon wont show anyway simple because android has removed it and didnt document anywere, i found another topic here talking about and the workaround – Rafael Lima Oct 21 '21 at 00:16
  • I couldn't even understand your code. it's not my fault my answer is wrong. Update the question and how you fixed it when you are done, so that people can also see it – Noah Oct 21 '21 at 04:17
  • what are the `receiverDismiss` and `receiverSnooze`? are they functions somewhere? – Shlomi Jul 28 '22 at 13:26
  • 1
    Sorry, @Shlomi, I'll update my code. They are both intents – Noah Jul 28 '22 at 22:22