この質問が何度も寄せられていることは承知しており、提供された解決策を実装しました。ただし、アプリを起動すると何度も通知が届き、何度も何度も繰り返します。24時間後に通知を受け取るように設定されています。助けてください。
onCreate では、以下の関数を呼び出しています。
private void setDailyCoins() {
Intent intent1 = new Intent(HomeActivity.this,AlarmReceiver.class);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(HomeActivity.this, 0, intent1, 0);
am.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE,02);
calendar.set(Calendar.SECOND, 02);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*60*60*1000, pendingIntent);
}
および通知の受信中:
public class AlarmReceiver extends BroadcastReceiver {
int MID;
private ParseQuery<ParseObject> query;
@Override
public void onReceive(Context context, Intent intent) {
long when = System.currentTimeMillis();
System.out.println("noti " + when);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context,
com.socialcurrency.teenpatti.LoginActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.teen_patti_icon)
.setContentTitle("ppp")
.setContentText(
"Please Collect 10,000 coins daily reward. Enjoy!!!")
.setSound(alarmSound).setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
calltoAddCoins(context);
}
マニフェストにレシーバーを追加しました
<receiver android:name=".AlarmReceiver" />