简体   繁体   English

Android:在特定日期安排通知消息

[英]Android : Schedule notification message at specific date

I'm looking to create a function for an Android app in which I get a notification every 25th day of the month indicating I have to do a certain task. 我希望为Android应用程序创建一个函数,在该函数中,每个月的第25天都会收到一条通知,指示我必须执行某些任务。

I've been able to display the notification using the following code : 我已经可以使用以下代码显示通知:

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

} }

Now this system only works when I have my app open and doesn't allow me to display this when the app is closed. 现在,此系统仅在我打开我的应用程序时有效,并且在我关闭应用程序时不允许我显示此系统。 I've searched around and came to this: Android notification at specific date After trying this out (the schedule part) I noticed that it doesn't work when I close the app, as I get an error about unregistering the Receiver, doing this (unregistering) results in the receiver being canceled, and the notification can not be showed. 我到处搜索并到达: 特定日期的Android通知尝试了此操作(计划部分)后,我发现关闭应用程序时该通知不起作用,因为我收到有关注销Receiver的错误,执行此操作(取消注册)会导致接收方被取消,并且通知不会显示。

code used for the schedule: 时间表使用的代码:

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

Is there anything I'm missing, or am I using the wrong methods to schedule a notification on a certain date? 我有什么想念的,还是我使用错误的方法在某个日期安排了通知? ( The current notification is set to be scheduled 15 seconds in the future, this is just for testing, I've got a function ready to display this at a certain date) (当前通知被设置为在未来15秒内安排,这仅用于测试,我已经准备好在特定日期显示此通知的功能)

This is used to notify on middle of the month. 这用于在月中通知。 Maybe You can get from below code. 也许您可以从下面的代码中获取。

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

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

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