gpt4 book ai didi

java - 在特定时间推送多个通知(AlarmManager、NotificationManager)- Android

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:22 24 4
gpt4 key购买 nike

我正在尝试制作离线推送通知,该通知将在特定时间推送一次。以下是我的日历和 AlarmManager 设置:

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 天的节日期间,我需要为每个 Activity 发送大约 30 条通知。

最佳答案

您使用的是哪个 API?

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

您可以在 AlarmManager docs 上找到更多信息,如果您仍然使用 AlarmManager,解决方案将是使用 setExactsetExactAndAllowWhileIdle

使用这些服务时要小心,如果您在短时间内收到多个通知,您可以考虑使用某项服务。

关于java - 在特定时间推送多个通知(AlarmManager、NotificationManager)- Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com