我要为我的应用程序创建多个警报。这是我的代码行:
public class NotificationAdministrationManager {
private Context context;
public NotificationAdministrationManager(Context context) {
this.context = context;
}
public void setAlarm(int cellId, Long timeStamp, String title, String message) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReceiverAdministration.class);
intent.putExtra(SettingsData.TAG_TITLE_NOTIFICATION, title);
intent.putExtra(SettingsData.TAG_MESSAGE_NOTIFICATION, message);
intent.putExtra(SettingsData.TAG_ID_NOTIFICATION, cellId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, cellId, intent, PendingIntent.FLAG_NO_CREATE);
am.set(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}
public void removeAlarm(int cellId) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(context,
MyReceiverAdministration.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, cellId, myIntent, PendingIntent.FLAG_NO_CREATE);
alarmManager.cancel(pendingIntent);
}
}
public class MyReceiverAdministration extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(SettingsData.TAG_TITLE_NOTIFICATION) &&
intent.hasExtra(SettingsData.TAG_MESSAGE_NOTIFICATION) &&
intent.hasExtra(SettingsData.TAG_ID_NOTIFICATION)) {
String title = intent.getStringExtra(SettingsData.TAG_TITLE_NOTIFICATION);
String message = intent.getStringExtra(SettingsData.TAG_MESSAGE_NOTIFICATION);
int cellId = intent.getIntExtra(SettingsData.TAG_ID_NOTIFICATION, 0);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentTitle(title)
.setContentText(message)
.setOngoing(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
Intent i = new Intent(context, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(
context,
cellId,
i,
PendingIntent.FLAG_NO_CREATE
);
mBuilder.setLights(0x6098CF, 1000, 2000);
//mBuilder.setSound(yourSoundUri);
mBuilder.setContentIntent(pendingIntent);
mNotifyMgr.notify(12345, mBuilder.build());
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
}
}
}
for (MyObject object : list) {
manager.removeAlarm(object.getId());
formatter.setCell(cell);
manager.setAlarm(object.getId(), object.getBegin(), formatter.formatTitle(), formatter.formatMessage());
}
一些有用的信息; object.getId()是一个唯一值,但在应用程序期间可以多次使用,但在该循环期间只能使用一次。我现在得到的是,在所有闹钟设置结束几秒钟后,我收到了唯一的最后通知。我的代码有什么问题?
答案 0 :(得分:0)
我使用过多个闹钟非常棘手。您的代码存在的问题是,您不会在Notification Manager上传递唯一ID,因为所有通知都会被覆盖。
mNotifyMgr.notify(unique_id_here, mBuilder.build());
以下是我在多个通知警报上实施的示例,其中我为每个警报设置了唯一的请求代码,并在通知管理器上设置了唯一ID。
public static void setAlarmManager(ArrayList<MedicationPlanEvent> events,Context context){
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
for(int i=0; i<events.size(); i++){
MedicationPlanEvent medicationPlan = events.get(i);
int request_code = 100 * medicationPlan.getId();
calendar.setTimeInMillis(System.currentTimeMillis());
if(medicationPlan.getTime().compareTo(TimeManager.getCurrentTime())<=0) calendar.add(Calendar.DAY_OF_MONTH,1);
String time_model[] = medicationPlan.getTime().split(":");
calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(time_model[0]));
calendar.set(Calendar.MINUTE,Integer.parseInt(time_model[1]));
int stop_request_code = request_code * 10;
Intent startNotificationAlarm = new Intent(context, NotificationService.class);
startNotificationAlarm.putExtra("Medicine", medicationPlan.getMedicine());
startNotificationAlarm.putExtra("ID",medicationPlan.getId());
startNotificationAlarm.putExtra("unique_id",request_code);
PendingIntent pendingNotificationIntent = PendingIntent.getService(context, request_code, startNotificationAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingNotificationIntent);
Intent stopNotificationAlarm = new Intent(context, StopAlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putInt("Notification", request_code);
bundle.putInt("MedicationPlanID",medicationPlan.getId());
bundle.putInt("StopNotification", stop_request_code);
stopNotificationAlarm.putExtras(bundle);
PendingIntent pendingStopNotificationIntent = PendingIntent.getBroadcast(context,stop_request_code, stopNotificationAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
int interval = 1000 * 60 * 60 * 24 * medicationPlan.getTotal_days();
calendar.setTimeInMillis(calendar.getTimeInMillis() + interval);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingStopNotificationIntent);
}
}
通知服务
@Override
protected void onHandleIntent(Intent intent) {
String medicine = intent.getExtras().getString("Medicine");
int id = intent.getExtras().getInt("ID");
int unique_id = intent.getExtras().getInt("unique_id");
//Every time the notification runs update the remaining days
RealmDatabase db = new RealmDatabase(this);
int days_left = db.updateMedicationPlanRemainingDays(id);
String title=medicine;
if(days_left!=-1){
if(days_left == 0) title += " ("+getString(R.string.last_day)+")";
else title += " (" +days_left+" "+getString(R.string.days_left)+")";
}
Log.d("Notification id ",unique_id+"");
//on notification click , launch recurrent transactions
Intent contentIntent = new Intent(this, OverviewActivity.class);
contentIntent.putExtra("flag",id);
PendingIntent pIntent = PendingIntent.getActivity(this, unique_id, contentIntent, 0);
//done action button
Intent doneIntent = new Intent(this, NotificationDoneReceiver.class);
doneIntent.putExtra("id", id);
doneIntent.putExtra("notification_id",unique_id);
PendingIntent donePendingIntent = PendingIntent.getBroadcast(this, (unique_id * 16), doneIntent, 0);
//notification builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.logo_white);
} else {
builder.setSmallIcon(R.drawable.logo);
}
//create the notification
Notification n = builder
//set title
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(0)
//set content
.setContentText(getString(R.string.time_for_medicine) + " " + medicine)
//set intent to launch on content click
.setContentIntent(pIntent)
//cancel notification on click
.setAutoCancel(true)
//add the actions
.addAction(R.drawable.ic_done, getString(R.string.took_it), donePendingIntent)
.build();
//Sound and vibrate
n.defaults |= Notification.DEFAULT_VIBRATE;
n.defaults |= Notification.DEFAULT_SOUND;
//notification manager
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//create the notification
notificationManager.notify(unique_id, n);
}