-1

UPDATE

I modified my code by updating the notification as advised by Pawel and Amit K. which I thank both. In the 'alarmNotification' method I create the notification with id1 and, when the button clicks, I pass the variable (string) "on" to the 'onReceive' of the 'Buttonlistener' class where I manage the new notification. Now the problem is that the string variable "alarm_state" passed to the onReceive does not correctly compare to the If construct. Can you tell me why?

public void alarmNotification(String title, String message)
{
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    notify.contentView=contentView;

    Intent button_intent=new Intent(context,ButtonListener.class);
    button_intent.putExtra("alarm_state","on");
    PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
    contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);

    nm.notify(1,notify);
}

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition=="on")  //here it's always false!!! Why??
        {                
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
            contentView.setTextViewText(R.id.subtitle,"Cam enabled");
        }
        if (condition=="off")
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }
}

OLD

In my app i have a personalized notification with a button that when pressed must change its text from "Active" to "Disabled". The notification is launched by a service and the click event triggers a class "ButtonListener" contained in the service that should change the text of the button. The problem is that the text of the button is not changed!!!

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        Toast.makeText(context, "Button clicked", Toast.LENGTH_SHORT).show();  // this ok
        try {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_notification, null);                
            Button button = (Button) layout.findViewById(R.id.switch1);
            button.setText("Disabled");   // this not update!!!
        }
        catch (Exception ec)
        {

        }

    }
}
Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35

3 Answers3

0

You inflate some new layout and it gets immediately garbage collected when onReceive finishes.

Sadly you have to create notification with updated view and notify NotificationManager to replace the old one.

Pawel
  • 15,548
  • 3
  • 36
  • 36
0

Update

 public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition.compareToIgnoreCase("on")==0)  //here it's always false!!! Why??
        {                
             contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
             contentView.setTextViewText(R.id.subtitle,"Cam enabled");
             Intent button_intent=new Intent(context,ButtonListener.class);
             button_intent.putExtra("alarm_state","off");
             PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,FLAG_UPDATE_CURRENT);
             contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
        }
        if (condition.compareToIgnoreCase("off")==0)
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
            Intent button_intent=new Intent(context,ButtonListener.class);
            button_intent.putExtra("alarm_state","on");
            PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
            contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }

old

You can not change the title of your action button after creating the notification already. But what you can do, you can set a background drawable which will be different based on state. something like this -

Lets say you have a my_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/button_on_image" android:state_pressed="true"/>
  <item android:drawable="@drawable/button_on_image" android:state_focused="true"/>
  <item android:drawable="@drawable/button_off_image"/>
</selector>

then -

addAction(R.drawable.my_button, <title>, <intent>);

Or you can do what @Pawel said, but thats creating another new notification replacing the old one.

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
  • Amit K, I tried your solution but the problem is that the button only changes until I hold it down while I want it to stay in that condition. – user10067214 Jul 21 '18 at 21:14
  • @user10067214, sorry I guess I misunderstood your problem. You would need to create a same notification with the same notification id but with the updated text/image in your buttons onReceive(). Perhaps if you could paste your notification creation code, we might be able to help you out more. – Amit K. Saha Jul 23 '18 at 15:34
  • Try this everywhere. PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,PendingIntent.FLAG_UPDATE_CURRENT); – Amit K. Saha Jul 23 '18 at 19:29
  • Now the variable 'condition' is no longer null and receives the correct value ( "on " or "off "), but the comparison ('condition ' == "on " or ' condition ' == "off ") strangely is not satisfied in fact, debugging immediately exits the conditional function: if (condition = = " On " {…..}) as if it were false but in reality it is not! – user10067214 Jul 23 '18 at 20:07
  • @user10067214, use condition.compareToIgnoreCase("on")==0 or condition.compareToIgnoreCase("off")==0 everywhere. If it solves your problem, kindly accept the anser – Amit K. Saha Jul 23 '18 at 20:14
  • Perfect! It works... thanks Amit K. What was wrong? – user10067214 Jul 23 '18 at 20:24
  • You need to learn more about string comparison in java. There are plenty of other post for string comparison (value vs reference). If my answers helped your problem, kindly accept the answer. It might help others in future. – Amit K. Saha Jul 23 '18 at 20:27
0

I modified my code by updating the notification as advised by Pawel and Amit K. which I thank both. In the 'alarmNotification' method I create the notification with id1 and, when the button clicks, I pass the variable (string) "on" to the 'onReceive' of the 'Buttonlistener' class where I manage the new notification. Now the problem is that the string variable "alarm_state" passed to the onReceive does not correctly compare to the If construct. Can you tell me why?

public void alarmNotification(String title, String message)
{
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    notify.contentView=contentView;

    Intent button_intent=new Intent(context,ButtonListener.class);
    button_intent.putExtra("alarm_state","on");
    PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
    contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);

    nm.notify(1,notify);
}

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition=="on")  //here it's always false!!! Why??
        {                
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
            contentView.setTextViewText(R.id.subtitle,"Cam enabled");
        }
        if (condition=="off")
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }
}