简体   繁体   English

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

[英]Pushing more than one notification at specific time (AlarmManager, NotificationManager) - Android

I'm trying to make offline push notifications that will be pushed once at the specific time. 我正在尝试制作离线推送通知,该通知将在特定时间推送一次。 Here are my Calendar and AlarmManager settings: 这是我的Calendar和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);

And my notification Class: 我的通知类别:

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 is just: RepeatingActivity.class只是:

public class RepeatingActivity extends AppCompatActivity{


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_program);

}

This code sends both notifications at 20:22 and at 20:28 and I want it separate, obviously. 这段代码在20:22和20:28发送两个通知,显然,我希望将它们分开。 So that one notifications goes on at 20:22 for example, and the other one at 20:28. 因此,例如,一个通知在20:22进行,另一个通知在20:28进行。 I will need around 30 notifications for every event during 3-day festival. 在为期3天的节日期间,每项活动我都需要大约30条通知。

Which api are you using? 您正在使用哪个API?

Note: as of API 19, all repeating alarms are inexact. 注意:从API 19开始,所有重复警报都是不精确的。 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. targetSdkVersion早于API 19的旧版应用程序将继续将其所有警报(包括重复警报)视为精确警报。

You can find more info on the AlarmManager docs , and the solution if you still use AlarmManager would be something with setExact or setExactAndAllowWhileIdle . 您可以在AlarmManager文档中找到更多信息,如果仍然使用AlarmManager,则解决方案将是setExactsetExactAndAllowWhileIdle

Be wary when using those, and if you have multiple notifications in a short amount of time, you may consider using a service. 使用这些通知时要当心,如果您在短时间内收到多个通知,则可以考虑使用一项服务。

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

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