【问题标题】:Android : Schedule notification message at specific dateAndroid:在特定日期安排通知消息
【发布时间】:2016-10-10 13:30:12
【问题描述】:

我希望为 Android 应用程序创建一个函数,在该函数中,我会在每个月的 25 天收到一条通知,指示我必须执行某项任务。

我已经能够使用以下代码显示通知:

public class NotificationPublisher extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    long[] pattern = {0, 300, 0};
    PendingIntent pi = PendingIntent.getActivity(context, 01234, intent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.small_logo_ico)
            .setContentTitle(context.getResources().getString(R.string.notification_title))
            .setContentText(context.getResources().getString(R.string.notification_content))
            .setVibrate(pattern)
            .setAutoCancel(true);

    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(01234, mBuilder.build());
}

}

现在这个系统只在我打开我的应用程序时工作,并且不允许我在应用程序关闭时显示它。我四处搜索并得出了这个结论: Android notification at specific date 在尝试了这个(计划部分)之后,我注意到当我关闭应用程序时它不起作用,因为我收到关于取消注册接收器的错误,这样做(取消注册)会导致接收器被取消,并且通知不能被展示。

用于日程安排的代码:

NotificationPublisher receiver = new NotificationPublisher();
    this.receiver = receiver;
    IntentFilter filter = new IntentFilter("ALARM_ACTION");
    registerReceiver(receiver, filter);

    Intent intent = new Intent("ALARM_ACTION");
    intent.putExtra("param", "My scheduled action");
    PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
    // I choose 15s after the launch of my application
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15000, operation) ;

我是否遗漏了什么,或者我使用了错误的方法来安排某个日期的通知? (当前通知设置为未来15秒后,这只是为了测试,我已经准备好在某个日期显示这个功能)

【问题讨论】:

标签: java android notifications alarm schedule


【解决方案1】:

这用于在月中通知。也许你可以从下面的代码中得到。

 Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 15);
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
            calendar.add(Calendar.DAY_OF_YEAR, 30);
        }
        Intent myIntent = new Intent(getApplicationContext(), MyReceiver.class);
        myIntent.putExtra("NOTI_MSG",getString(R.string.notification_sidas));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), NOTI_REQ_CODE_SIDAS, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY * 30, pendingIntent);
    }

【讨论】:

  • 我已将最后一部分更改为:“alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+3000, 2000 , pendingIntent);”并保持其余部分相同(当然在必要时更改参考),有什么原因无法正常工作?
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2021-12-29
  • 2015-03-28
相关资源
最近更新 更多