因此,我每天早上10点左右尝试向用户发送通知。用户可以选择取消每日通知。
我面临一些问题。我在上午10点发出通知。我收到了。但是,它会在几秒钟或几分钟后抛出另一个通知。 我试图在一个单独的可运行线程中设置此通知。我能够成功实施取消选项,但我遇到了这个多重通知问题。在这里,我附上了部分代码:
class Signup implements Runnable {
@Override
public void run() {
Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, 10);
updateTime.set(Calendar.MINUTE, 03);
updateTime.set(Calendar.SECOND,10);
Intent notification = new Intent(MainActivity.this, Alert.class);
PendingIntent recurringNotification = PendingIntent.getBroadcast(MainActivity.this,
0, notification, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) MainActivity.this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringNotification);
}
}
接收者:
public class Alert extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
dailyNotif(context,"title","Your Notif is here. Click to view.",
" here.");
}
public void dailyNotif(Context context, String title, String body, String alert){
Intent notifyIntent = new Intent(context,AnotherNotif.class);
PendingIntent notification = PendingIntent.getActivity(context,0,notifyIntent,0);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setTicker(alert)
.setContentText(body);
builder.setContentIntent(notification);
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
builder.setAutoCancel(true);
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
答案 0 :(得分:0)
弄清楚它为什么会随机发出通知。
如果时间已经过去,它会在您运行应用程序时立即发出通知。因此,在这种情况下,如果
,您需要在闹钟时间添加一天当前时间>闹钟时间