如何每5分钟发送一次通知?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何每5分钟发送一次通知?相关的知识,希望对你有一定的参考价值。

我希望从用户每隔5分钟发送给用户的重复通知,只要用户不接受它们。而他什么时候我想取消闹钟。

这是我的代码,但我不知道如何处理警报管理器和日历:

acceptBtnPendingIntent = PendingIntent.getBroadcast(this, 123, acceptBtnIntent, 0);
remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
remoteView.setOnClickPendingIntent(R.id.acceptCallBtn, acceptBtnPendingIntent);
remoteView.setImageViewResource(R.id.notificationIcon, R.drawable.notification_icon);
remoteView.setTextViewText(R.id.fromTxt, fromId);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 60 * 20, acceptBtnPendingIntent);

mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
    .setCustomHeadsUpContentView(remoteView)
    .setDefaults(Notification.DEFAULT_ALL)
    .setContentIntent(acceptBtnPendingIntent)
    .setAutoCancel(true)
    .setPriority(NotificationManager.IMPORTANCE_HIGH);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationsId, mBuilder.build());
答案

希望这可以适用于您的情况。

//write the code where you would like to create and call alarm repetitively

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    Intent intent = new Intent(context,AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234/*unique request code*/, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),5 * 60 * 1000 /*5 min in millis*/, pendingintent);

//your Broadcast Receiver
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
        //Acquire the lock
        wl.acquire();
showNotification(context);

  if (intent.getAction().equals("USER_ACCEPTED")) {

    //TODO: do your action you would like to do: do cancellation of alarm.. code here!
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context,AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234/*unique request code*/, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();

      }

}

public void showNotification(){

mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
    .setDefaults(Notification.DEFAULT_ALL)
    .setAutoCancel(true)
    .setPriority(NotificationManager.IMPORTANCE_HIGH);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationsId, mBuilder.build());

}

我已经给出了可以帮助你的代码。让我知道我是不是错了。

以上是关于如何每5分钟发送一次通知?的主要内容,如果未能解决你的问题,请参考以下文章

当 Google Play 服务无法获得位置修复时,我如何获得通知?

如何使用 PHP 每分钟向电报机器人发送通知?

如何每10秒发送一次通知?

本地通知 UNTimeIntervalNotificationTrigger triggerWithTimeInterval 每 1 分钟触发一次 如何停止

iOS 10 - 每“x”分钟重复一次通知

iOS 11-每 x 分钟重复一次的用户本地通知