1

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

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

这是我在主要活动中设置警报管理器的代码:

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);


}

以及我的广播接收器中的代码:

公共类 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);
}

}

4

1 回答 1

0

好吧,我想通了。

问题是 FLAG_ONE_SHOT;它应该是 FLAG_UPDATE_CURRENT。

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

瞧!

于 2012-10-21T21:19:32.937 回答