繁体   English   中英

通知不重复

[英]notification not repeating

我为我的应用程序创建了一个通知。 它在正确的时间推送就可以正常工作,并且在按下时链接到正确的活动。

但是,我用重复的警报来称呼它是因为我希望它在特定的日子里发射出去。 在我的初始测试中,我将其设置为每5秒按下一次,以便可以快速检查它是否正确重复。 初次推送后,一旦我清除它,该通知就不会再出现。

这是我设置alarmManager的主要活动中的代码:

private void notificationAlarm() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK, 1);
    cal.set(Calendar.HOUR, 1);
    cal.set(Calendar.MINUTE, 40);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    long interval = cal.getTimeInMillis()+5000;

    Intent alarmIntent = new Intent(this, alarmNotif.class);
    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager notifAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //notifAlarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
    notifAlarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), interval, alarmPendingIntent);


}

以及我的broadcastreceiver中的代码:

公共类AlarmNotif扩展了BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String title = "Don't forget to order sushi from Arbuckle!";
    String subTitle = "Order before 10 AM with Arbuckle App";
    Intent notifIntent = new Intent(context, SecureAppStarter.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
    .setContentTitle(title)
    .setContentText(subTitle)
    .setSmallIcon(R.drawable.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setContentIntent(pendingIntent);

    Notification notif = notifBuilder.getNotification();
    notifManager.notify(1, notif);
}

}

好吧,我想通了。

问题是FLAG_ONE_SHOT; 它应该是FLAG_UPDATE_CURRENT。

这允许以期望的间隔重复通知。

瞧!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM