如何更新通知?

how to update notification?

我有一个带有文本视图的自定义通知,it.I 处有一个按钮,希望在单击该按钮时更改 TextView 的文本。 我做了一个广播,当点击按钮时程序开始运行那个广播,广播代码是:

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);

    }


}

如您所见,我从 RemoteView 创建了一个对象并将选定的文本放入 TextView,但我不知道如何将此更改更新到通知面板。

下面的代码解释了如何创建您自己的通知:

    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);

此外,如果您想添加远程视图,您可以使用方法:

setContent(RemoteViews remoteView)

来自 Notification.Builder class