私はアプリケーションを作成していますが、このアプリケーションでは、ユーザーが通知をタップするたびにアプリケーションを開くのと同じように、同じ意図で複数の通知を設定する必要があります。
すべての通知はデータなしで異なる日時に送信されますが、問題は、午後 3 時 27 分と午後 3 時 28 分に 2 つの通知を設定すると、最初の通知 (午後 3 時 27 分) がキャンセルされることです (2 番目に上書きされます)。そして2番目は正しく機能しています。
私は現在、この目標を達成するためにこのコードを使用しています:
このメソッドは、Activity からの通知を設定するために使用されます。
public static void setNotificationOnDateTime(Context context, long fireTime)
{
int requestID = (int) System.currentTimeMillis();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, NotificationReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, requestID, i, 0);
am.set(AlarmManager.RTC_WAKEUP, fireTime, pi);
}
私の NotificationReceiver クラスは次のようになります。
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "Notification fired...");
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent;
Bundle bundle = intent.getExtras().getBundle("NotificationBundle");
if(bundle == null)
{
contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, SplashScreen.class), 0);
}
else
{
contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MenuScreen.class)
.putExtra("NotificationBundle", bundle), 0);
}
Notification notif = new Notification(R.drawable.ic_launcher,
"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, "Me", "Message test", contentIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
私はすでにグーグルに多くの時間を費やし、いくつかの解決策を見つけましたが、それらは私にとってはうまくいきませんでした。それらのいくつかのリンクは次のとおりです。
誰かがこれを行う方法を知っているなら、私を助けてください。