通知不重复

时间:2012-10-21 08:50:56

标签: android notifications alarmmanager

我为我的应用制作了通知。它正在工作,从某种意义上讲它是在正确的时间推动,并在按下时链接到正确的活动。

然而,我称之为重复警报,因为我希望它在特定日子拍摄。 在我的初始测试中,我将其设置为每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中的代码:

public class 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);
}

}

1 个答案:

答案 0 :(得分:0)

好吧我明白了。

问题是FLAG_ONE_SHOT;它应该是FLAG_UPDATE_CURRENT。

这允许以所需的间隔重复通知。

瞧!