2016-10-10 55 views
0

我期待爲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秒,這僅用於測試,我有一個功能準備在某個日期顯示此功能)

+1

http://stackoverflow.com/questions/31086226/show-a-notification-on-a-particular-date-and-time – HAXM

回答

0

這是用來通知月中。也許你可以從下面的代碼中獲得。

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

我已經改變了最後一部分:「alarmManager.setRepeating( AlarmManager.RTC,System.currentTimeMillis()+ 3000, 2000,pendingIntent);「並保持其餘的不變(當然,必要時改變引用),爲什麼這種方式無法正常工作? – Gerton