我期待爲Android應用程序創建一個函數,在該應用程序中,每月的第25天我會收到通知,指示我必須執行某個任務。Android:在特定日期安排通知消息
我已經能夠使用下面的代碼顯示通知:
public class NotificationPublisher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long[] pattern = {0, 300, 0};
PendingIntent pi = PendingIntent.getActivity(context,, 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(, mBuilder.build());
}
}
現在,當我有我的應用程序打開並不允許我顯示這個這個系統只適用當應用程序關閉時。我搜索了四處,並來到這個: Android notification at specific date 試了一下(時間表部分)後,我注意到,它關閉應用程序時不起作用,因爲我得到一個關於註銷Receiver的錯誤,這樣做(取消註冊)導致接收器被取消,並且不能顯示通知。用於日程
代碼:
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秒,這僅用於測試,我有一個功能準備在某個日期顯示此功能)
http://stackoverflow.com/questions/31086226/show-a-notification-on-a-particular-date-and-time – HAXM