如何在 Android 中使用 AlarmManager 设置多个通知
How to set multiple notifications using AlarmManager in Android
我有一个包含项目的列表,用户可以在每个项目中为其设置通知,然后从通知中打开它。它工作正常,但我只能设置一个通知,无论它来自同一项目还是另一个项目都没有关系。如果我设置了多个,它只会显示最后一个。我不知道我还能做什么。这是我的代码
在项目详细信息中 class
Long alertTime = new GregorianCalendar().getTimeInMillis() + 7 * 1000;
Intent alertIntent = new Intent(getApplicationContext(), AlertReceiver.class);
alertIntent.putExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(getApplicationContext(), 1, alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
接收方:
public class AlertReceiver extends BroadcastReceiver {
int idAplicacion;
@Override
public void onReceive(Context context, Intent intent) {
idAplicacion = intent.getIntExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);
Log.e("TESTING", "the id is " + String.valueOf(idAplicacion));
createNotification(context, "some title", "some message text", "some sticker");
}
public void createNotification(Context context, String title, String msgText, String sticker)
{
Intent i = new Intent(context, DetalleAplicacionActivity.class);
i.putExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);
PendingIntent notificIntent = PendingIntent.getActivity(context,0, i ,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msgText)
.setTicker(sticker)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource( context.getResources(), R.drawable.ic_launcher));
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NotificationID.getID(), mBuilder.build());
}
}
和
public class 通知 ID {
private final static AtomicInteger c = new AtomicInteger(0);
public static int getID() {
return c.incrementAndGet();
}
}
清单
<uses-permissionandroid:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name=".Clases.AlertReceiver"/>
原因是您创建了具有相同 requestCode
的通知 - 所以它们都受到 Android 的威胁,因为同一个通知,您只需更新它即可。
创建通知时使用一些变化
public void createNotification(Context context, String title, String msgText, String sticker) {
...
PendingIntent notificIntent = PendingIntent.getActivity(context,
java.util.Random.nextInt(100000), i ,PendingIntent.FLAG_CANCEL_CURRENT);
...
}
我有一个包含项目的列表,用户可以在每个项目中为其设置通知,然后从通知中打开它。它工作正常,但我只能设置一个通知,无论它来自同一项目还是另一个项目都没有关系。如果我设置了多个,它只会显示最后一个。我不知道我还能做什么。这是我的代码
在项目详细信息中 class
Long alertTime = new GregorianCalendar().getTimeInMillis() + 7 * 1000;
Intent alertIntent = new Intent(getApplicationContext(), AlertReceiver.class);
alertIntent.putExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(getApplicationContext(), 1, alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
接收方:
public class AlertReceiver extends BroadcastReceiver {
int idAplicacion;
@Override
public void onReceive(Context context, Intent intent) {
idAplicacion = intent.getIntExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);
Log.e("TESTING", "the id is " + String.valueOf(idAplicacion));
createNotification(context, "some title", "some message text", "some sticker");
}
public void createNotification(Context context, String title, String msgText, String sticker)
{
Intent i = new Intent(context, DetalleAplicacionActivity.class);
i.putExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);
PendingIntent notificIntent = PendingIntent.getActivity(context,0, i ,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msgText)
.setTicker(sticker)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource( context.getResources(), R.drawable.ic_launcher));
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NotificationID.getID(), mBuilder.build());
}
}
和 public class 通知 ID {
private final static AtomicInteger c = new AtomicInteger(0);
public static int getID() {
return c.incrementAndGet();
}
}
清单
<uses-permissionandroid:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name=".Clases.AlertReceiver"/>
原因是您创建了具有相同 requestCode
的通知 - 所以它们都受到 Android 的威胁,因为同一个通知,您只需更新它即可。
创建通知时使用一些变化
public void createNotification(Context context, String title, String msgText, String sticker) {
...
PendingIntent notificIntent = PendingIntent.getActivity(context,
java.util.Random.nextInt(100000), i ,PendingIntent.FLAG_CANCEL_CURRENT);
...
}