1
我試圖讓離線推送通知在特定時間推送一次。 這裏是我的日曆和AlarmManager設置:在特定時間推送多個通知(AlarmManager,NotificationManager) - Android
Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 102, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar push1 = Calendar.getInstance();
Calendar push2 = Calendar.getInstance();
push1.set(2017, Calendar.JULY, 1);
push1.set(Calendar.HOUR_OF_DAY, 20);
push1.set(Calendar.MINUTE, 22);
push1.set(Calendar.SECOND, 30);
push2.set(2017, Calendar.JULY, 1);
push2.set(Calendar.HOUR_OF_DAY, 20);
push2.set(Calendar.MINUTE, 28);
push2.set(Calendar.SECOND, 30);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, push1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, push2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
我的通知類:
public class Notification_receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, RepeatingActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 101, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 102, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent1)
.setSmallIcon(R.drawable.onusicijadi_icon)
.setContentTitle("FIRST")
.setContentText("FIRST NOTIFICATION")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
notificationManager.notify(103, builder1.build());
NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent2)
.setSmallIcon(R.drawable.onusicijadi_icon)
.setContentTitle("SECOND")
.setContentText("SECOND NOTIFICATION")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
notificationManager.notify(104, builder2.build());
}}
RepeatingActivity.class就是:
public class RepeatingActivity extends AppCompatActivity{
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program);
}
此代碼在20:22發出兩份通知並在20點28分,我希望它分開,顯然。因此,一個通知在20:22發生,例如20:28發生。 在3天的節日期間,我將需要大約30個每個事件的通知。
當我使用'setExact'時,兩個通知在同一時間和之後到達,它們都會再次到達。我想知道我怎樣才能一次發送第一張,然後再發送第二張?爲什麼兩個實例(push1和push2)都到達只有push1應該到達的時間? –
@OgiScekic甚至setExact有問題,因爲它閒置時不運行。讓它真正匹配你指定的時間需要'setExactAndAllowWhileIdle'但是API 23以上。在這種情況下,您可以考慮通過JobScheduler或其他方法啓動的服務,該服務可以保持活動狀態持續一段時間,以便在確切的時間發出通知。 –