我已经实现了一个处理通知的应用程序,这些通知是以编程方式设置的,具有以下代码:
public void setAlarm(Cell cell, Long timeStamp, String title, String message) {
Integer id = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(context, receiver);
notificationIntent.putExtra("ID", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, notificationIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeStamp, 2 * 60 * DateUtils.SEC_TO_MILLIS, pendingIntent);
RepositoryManager repositoryManager = RepositoryManager.getInstance();
repositoryManager.storeNotificationAdministration(new NotificationAdministration(id, timeStamp, 2, title, message, 1, null, cell, StatusNotification.NOT_YET_NOTIFIED.getId()));
}
这是我的接收者:
@Override
public void onReceive(Context context, Intent intent) {
RepositoryManager controllerDatabase = RepositoryManager.getInstance(ControllerDatabase.getInstance(context));
NotificationMedicine notificationMedicine = controllerDatabase.getNotificationMedicine();
if (notificationMedicine.getEnable()) {
if (intent.hasExtra("ID")) {
int id = intent.getIntExtra("ID", 0);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("DEBUG", "MyReceiverAdministration " + id);
try {
NotificationAdministration notificationAdministration = controllerDatabase.findNotificationAdministrationById(id);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_medicine)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentTitle(notificationAdministration.getTitle())
.setContentText(notificationAdministration.getMessage())
.setOngoing(false)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(false);
Intent i = new Intent(context, MainActivity.class);
i.putExtra(SettingsData.TAG_CELL, notificationAdministration.getCell().getId());
PendingIntent pendingIntent =
PendingIntent.getActivity(
context,
notificationAdministration.getId(),
i,
0
);
Intent snoozeIntent = new Intent(context, MyFlagBroadcastReceiver.class);
snoozeIntent.putExtra(SettingsData.TAG_CELL, notificationAdministration.getId());
PendingIntent flagIt =
PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
mBuilder.setLights(0x6098CF, 1000, 2000);
Uri uriRingtone = notificationMedicine.getRingtone();
mBuilder.setSound(uriRingtone);
mBuilder.setContentIntent(pendingIntent);
mBuilder.addAction(R.drawable.ic_timer_black_24dp, context.getString("Do my action"), flagIt);
mNotifyMgr.notify(notificationAdministration.getId(), mBuilder.build());
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(700);
notificationAdministration.setStatus(StatusNotification.NOTIFIED.getId());
controllerDatabase.storeNotificationAdministration(notificationAdministration);
} catch (ObjectNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
这是其他Receiver,当用户点击通知的操作时:
RepositoryManager controllerDatabase = RepositoryManager.getInstance(ControllerDatabase.getInstance(context));
if (intent.hasExtra(SettingsData.TAG_CELL)) {
int id = intent.getIntExtra(SettingsData.TAG_CELL, 0);
try {
NotificationAdministration notificationAdministration = controllerDatabase.findNotificationAdministrationById(id);
Device device = controllerDatabase.getDefaultDeviceByUser(controllerDatabase.getLoggedUser().getUser());
Cell cell = controllerDatabase.findCellById(device, notificationAdministration.getCell().getId());
Calendar calendar = Calendar.getInstance();
Long now = calendar.getTimeInMillis() / DateUtils.SEC_TO_MILLIS;
controllerDatabase.flagMedicineTaken(cell, now);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.cancel(id);
}
} catch (ObjectNotFoundException e) {
e.printStackTrace();
}
}
我面临的问题是,MyFlagBoradcastReceiver始终与通知的ID相同,而不是我按下/点击的ID。
该应用程序的想法是设置不同的通知,这些通知始终是可见的,用户点击通知的操作是徒劳的。警报也应该在2分钟后触发......
**更新**
这是我应该用来取消闹钟的代码行;我该怎么用作接收器?
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(context,
receiver);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, notificationAdministration.getId(), myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (alarmManager != null) {
alarmManager.cancel(pendingIntent);
}
答案 0 :(得分:0)
为PendingIntent
创建Notification
时,您需要执行以下操作:
PendingIntent flagIt = PendingIntent.getBroadcast(context,
notificationAdministration.getId(),
snoozeIntent, 0);
您需要确保为PendingIntent
创建的每个MyFlagBroadcastReceiver
都是唯一的,否则Android将返回现有的PendingIntent
而不是创建新的PendingIntent.getBroadcast()
。您可以通过为request.getSession().getAttribute("user");
的每次通话提供唯一ID来执行此操作。