Morteza
Morteza

Reputation: 51

how to update notification?

I had a custom notification with a text view and a button at it.I want to change the text of TextView when the button clicked. I made a broadcast that when the button clicked the program start to run that broadcast,the codes of broadcast is :

package com.mori.sepid.notifications;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.util.Log;
 import android.view.ViewGroup;
 import android.app.Notification;
 import android.widget.RemoteViews;
 import android.app.NotificationManager;
 import android.widget.TextView;

 public class button_broadcast_resiver extends BroadcastReceiver {
   public button_broadcast_resiver() {
   }

    @Override
    public void onReceive(Context context, Intent intent) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.customnoti1);
        remoteViews.setTextViewText(R.id.text,"Hi morteza");
        NotificationManager noti=
        (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

    }


}

as you see the code above,I made an object from RemoteView and put the selected text to TextView but I don't know how to update this change to the notification panel.

Upvotes: 1

Views: 643

Answers (1)

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

The code below explains how to create your own notification:

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification notification = new NotificationCompat.Builder(context)
            //here you set icon which will be displayed on top bar
            .setSmallIcon(R.drawable.your_ic_notification)
            //here is title for your notification
            .setContentTitle("tite"/*your notification title*/)
            //here is a content which will be displayed when someone expand action bar
            .setContentText("Some example context string"/*notifcation message*/)
            .build();
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(1000/*some int notification id*/, notification);

Additionally if you want to add your Remote view you can use method:

setContent(RemoteViews remoteView)

from Notification.Builder class

Upvotes: 1

Related Questions