私は今、これを正確に行う方法を何時間も探しています:
アプリが既に開いている場合を除いて、毎日 (週末を除く) 通知が一度に送信されるようにしたい (18:00 (= 午後 6 時) としましょう)。メールを受信するときは、基本的にGmailアプリのようなものです。ユーザーが通知をクリックすると、通知が消えて MainActivity に移動する必要があります。
AlarmManager でさまざまなことを試しましたが、通知が表示されることはありませんでした。
私が試したコードは、かなり正しいと感じていますが、次のとおりです。
AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
私の通知サービス:
public class NotificationService extends IntentService {
public NotificationService() {
super("NotificationService");
}
@Override
@SuppressWarnings("deprecation")
protected void onHandleIntent(Intent intent) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
nm.notify(1, notification);
}
}
これは、AlarmManager を使用していないときに通知が表示される唯一の方法でさえあったため、非推奨のものを使用していることに注意してください。可能であれば、非推奨のものを含まないソリューションで対応してください。ただし、最新のものを使用してください:P.
事前に多くの多くの多くのおかげで!!!
よろしくお願いします