Java 更新现有通知的方法?
嗨,我希望通知的行为如下:Java 更新现有通知的方法?,java,android,notifications,Java,Android,Notifications,嗨,我希望通知的行为如下: Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(Constants.SOME_DATA, someData); Pe
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.SOME_DATA, someData);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
当我的应用程序中并没有单个通知时,应该创建一个通知,但当托盘中已经有通知时,我想更新通知的数据
我可以得到当前的通知或任何其他方式吗
另外,我有一个单一类型的UI+数据用于通知,它在特定事件发生时频繁触发
e、 g.如果应用程序每5分钟收到一次关于比赛分数的消息,那么当前通知应根据收到的最后一条消息进行更新,则不应对每条分数更新消息进行通知
我的代码如下:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.SOME_DATA, someData);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
如果您想永久运行单个通知并更新该通知,则必须使用
startForeground(通知ID,通知)创建前台通知代码>
然后,您可以使用相同的notification\u ID
更新通知,其中您的案例为0/*ID of notification*/或者如果您想要简单的通知,则可以使用相同的notification\u ID
更新通知,并且如果您想要运行单个永久更新通知,而不必使用startForeground(通知ID,通知)创建前台通知代码>
然后,您可以使用相同的notification\u ID
更新通知,其中您的案例为0/*ID of notification*/或者如果您想要简单的通知,则可以使用相同的notification\u ID
更新通知,并在挂起的意图中保持FLAG\u update\u当前
并始终使用相同的通知id。
在本例中为0。
它将更新您当前的通知
试试这个演示
public class MainActivity extends Activity {
private ImageView ImageButton;
int value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton = (ImageView) findViewById(R.id.imageButton);
ImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value++;
UpdateNow(value + "");
}
});
}
void UpdateNow(String msg) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("asd")
.setContentText(msg)
.setSound(defaultSoundUri)
.setOngoing(true)
.setContentIntent(null);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
设置标志.setContinuous(true)
并始终使用相同的通知id。
在本例中为0。
它将更新您当前的通知
试试这个演示
public class MainActivity extends Activity {
private ImageView ImageButton;
int value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton = (ImageView) findViewById(R.id.imageButton);
ImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value++;
UpdateNow(value + "");
}
});
}
void UpdateNow(String msg) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("asd")
.setContentText(msg)
.setSound(defaultSoundUri)
.setOngoing(true)
.setContentIntent(null);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
@沙阿,如果您有任何问题,请在这里发表评论。@Shhali Zahid感谢您的回复,并对我的回复迟到表示歉意。我尝试了你的建议,但仍然无法生成新通知。注意:关闭时它正在生成新通知,否则它正在更新通知。@HarshShah关闭时是什么意思?@HarshShah关闭时它正在生成新通知,否则它正在更新通知。请详细说明这条线<代码>当它关闭的你正在考虑我们的APP通知吗?@ SoHaul-ZaHID考虑一个应用程序正在运行。我正在发送第一个通知,设备正在显示一个通知,然后我再次发送相同的通知,然后它正在更新通知。第二种情况是考虑我的应用程序关闭了,我发送第一个通知应用程序正在显示一个通知,然后我再次发送相同的通知,现在应用程序没有更新通知,它正在生成新的通知。@ AdthShanh如果这里有一些问题的评论。@ SHARI ZHID谢谢回复,抱歉我迟了回复。我尝试了你的建议,但仍然无法生成新通知。注意:关闭时它正在生成新通知,否则它正在更新通知。@HarshShah关闭时是什么意思?@HarshShah关闭时它正在生成新通知,否则它正在更新通知。请详细说明这条线<代码>当它关闭的你正在考虑我们的APP通知吗?@ SoHaul-ZaHID考虑一个应用程序正在运行。我正在发送第一个通知,设备正在显示一个通知,然后我再次发送相同的通知,然后它正在更新通知。第二种情况是现在考虑我的应用程序是关闭的我发送第一个通知应用程序正在显示一个通知,然后我再次发送相同的通知现在应用程序没有更新通知它正在生成新的通知。