使用 SQL 更新多个本地通知
Updating multiple local notifications with SQL
您好,目前我已经设置了一个允许用户设置闹钟的应用程序。当此警报到达指定时间时,它将向用户发送通知。目前,每次激活此通知时,它都会发送一条静态消息,例如"You have a notification"。我希望能够将此通知标题更改为当用户发出警报时我存储在 SQLOPENHELPER 中的警报名称。
我还为每个警报指定了一个特定的 ID,即当前时间(以毫秒为单位),然后也将其存储在我的 SQL 中。现在我知道每个通知都需要一个新 ID,但我该怎么做并将其关联回我的 SQL?
我附上了一张小图,只是为了阐明我在做什么以及我当前的警报和广播接收器...
图表......
设置闹钟......
// id - 与为该警报提供的自定义 id 相关
// 接收器 - 是我们的意图
pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
// Intent数组列表用于存放多个告警
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);
// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
// Notification ID 已保留为 0,因为此时我不知道该怎么做,在尝试弄清楚 2 天后我想我应该问一下。
////更新
主要 Activity
我已经在我的意图 Array List 下实现了一个 put extra
receiver.putExtra("title", id);
广播
Bundle bundle = intent.getExtras();
if (bundle != null) {
pass = bundle.getString("title");
//I have put my notification in this section
}
我通过将 "pass"(用于检索 put extra 的字符串)放入我的通知标题中进行了测试,结果显示为空白?
//主要activity
receiver.putExtra("title", id);
pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
广播
Bundle bundle = intent.getExtras();
int pass = bundle.getInt("title" , 1);
谢谢 psKink,你给了我需要的动力
如下更新您的接收器意图以传递 ALARM_ID
:
Intent receiver = new Intent(getApplicationContext(), Receiver.class);
receiver.putExtra("KEY_ALARM_ID", ALARM_ID); // ALARM_ID = id
在PendingIntent.getBroadcast()
中,使用ALARM_ID
作为requestCode
。
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, receiver, PendingIntent.FLAG_UPDATE_CURRENT );
Always use different requestCode
for different notification to get
correct result.
在notify()
方法中,使用ALARM_ID
作为id
:
notificationManager.notify(ALARM_ID, builder.build());
Notification id
should be unique within your application. If a
notification with the same id
has already been posted by your
application and has not yet been canceled, it will be replaced by the
updated information.
在您的 BroadcastReceiver class onReceive()
方法中获取 ALARM_ID
:
@Override
public void onReceive(Context context, Intent intent)
{
int alarmId= intent.getExtras().getInt("KEY_ALARM_ID");
// Do something with alarmId
}
希望这能解决您的问题。
您好,目前我已经设置了一个允许用户设置闹钟的应用程序。当此警报到达指定时间时,它将向用户发送通知。目前,每次激活此通知时,它都会发送一条静态消息,例如"You have a notification"。我希望能够将此通知标题更改为当用户发出警报时我存储在 SQLOPENHELPER 中的警报名称。
我还为每个警报指定了一个特定的 ID,即当前时间(以毫秒为单位),然后也将其存储在我的 SQL 中。现在我知道每个通知都需要一个新 ID,但我该怎么做并将其关联回我的 SQL?
我附上了一张小图,只是为了阐明我在做什么以及我当前的警报和广播接收器...
图表......
设置闹钟......
// id - 与为该警报提供的自定义 id 相关 // 接收器 - 是我们的意图
pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
// Intent数组列表用于存放多个告警
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);
// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
// Notification ID 已保留为 0,因为此时我不知道该怎么做,在尝试弄清楚 2 天后我想我应该问一下。
////更新 主要 Activity
我已经在我的意图 Array List 下实现了一个 put extra
receiver.putExtra("title", id);
广播
Bundle bundle = intent.getExtras();
if (bundle != null) {
pass = bundle.getString("title");
//I have put my notification in this section
}
我通过将 "pass"(用于检索 put extra 的字符串)放入我的通知标题中进行了测试,结果显示为空白?
//主要activity
receiver.putExtra("title", id);
pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
广播
Bundle bundle = intent.getExtras();
int pass = bundle.getInt("title" , 1);
谢谢 psKink,你给了我需要的动力
如下更新您的接收器意图以传递
ALARM_ID
:Intent receiver = new Intent(getApplicationContext(), Receiver.class); receiver.putExtra("KEY_ALARM_ID", ALARM_ID); // ALARM_ID = id
在
PendingIntent.getBroadcast()
中,使用ALARM_ID
作为requestCode
。PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, receiver, PendingIntent.FLAG_UPDATE_CURRENT );
Always use different
requestCode
for different notification to get correct result.
在
notify()
方法中,使用ALARM_ID
作为id
:notificationManager.notify(ALARM_ID, builder.build());
Notification
id
should be unique within your application. If a notification with the sameid
has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
在您的 BroadcastReceiver class
onReceive()
方法中获取ALARM_ID
:@Override public void onReceive(Context context, Intent intent) { int alarmId= intent.getExtras().getInt("KEY_ALARM_ID"); // Do something with alarmId }
希望这能解决您的问题。