Android 更新通知文本
我目前已经安装了一个小应用程序,它在设备上设置警报,并将相关信息存储在SQL数据库中,例如警报名称和时间。当警报被激活时,我的应用程序会向用户发送通知。当前,每个通知都是静态的,这意味着每个消息都是相同的。现在,我想允许我的应用程序获取已激活的报警名称,并在通知中显示它。现在我知道,当设置多个报警时,您需要多个ID,我已经将这些ID存储在我的SQL中,我想在发送通知时也是这样。是否有方法将我的报警ID与通知上的报警ID匹配,以便它知道要发送的消息,例如报警名称 设置我的闹钟的当前代码Android 更新通知文本,android,sql,android-intent,notifications,alarm,Android,Sql,Android Intent,Notifications,Alarm,我目前已经安装了一个小应用程序,它在设备上设置警报,并将相关信息存储在SQL数据库中,例如警报名称和时间。当警报被激活时,我的应用程序会向用户发送通知。当前,每个通知都是静态的,这意味着每个消息都是相同的。现在,我想允许我的应用程序获取已激活的报警名称,并在通知中显示它。现在我知道,当设置多个报警时,您需要多个ID,我已经将这些ID存储在我的SQL中,我想在发送通知时也是这样。是否有方法将我的报警ID与通知上的报警ID匹配,以便它知道要发送的消息,例如报警名称 设置我的闹钟的当前代码 pendi
pendingIntent = PendingIntent.getBroadcast(view.getContext(), Integer.valueOf(alarm_request_code), receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
我的广播接收器
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Standard text")
.setContentText("Standard text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
}
//////////////
更新我的情况,这正慢慢让我发疯
现在,我可以将SQL中的文本设置为通知,但每次都是数据库中的第一个文本。现在我提出了两种可能的解决方案,它们只是理论上的
更新文本存储
mBuilder
变量
并在以后以这种方式更新:
mBuilder.setContentTitle(head);
mBuilder.setContentText(正文);
mNotificationManager.notify(SIMPLE_ID,mBuilder.build())代码>
编辑
private void simple() {
Intent intent = new Intent(context, AlarmActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setContentIntent(pIntent);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.ic_done_24dp);
} else {
mBuilder.setSmallIcon(someiconLollipop);
mBuilder.setColor(somecolorLollipop]);
}
noti = mBuilder.build();
//use flags if you need:
noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
根据这些文档,您可以执行以下操作:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
这里的诀窍是通知id,在您的案例0中,它必须相同才能正常工作
因此,只要它没有被撤销,它就应该相应地发挥作用。我建议将唯一id作为定制通知(如123)的常量
notificationManager.notify(0, builder.build());
谢谢,我可以有一点更详细的请。谢谢你的明确回答,我将尝试并建立在它的基础上应用我的SQL数据库。感谢you@Steven,如果您愿意,请不要忘记批准答案helpfil@Steven,我认为最好的办法是创造一个新的问题。因为它们完全不同。@Steven,顺便说一句,我认为最有用的解决方案是创建一个包装器:SQLite+通知更新程序。
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
notificationManager.notify(0, builder.build());