简体   繁体   中英

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:

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:

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. So that one notifications goes on at 20:22 for example, and the other one at 20:28. I will need around 30 notifications for every event during 3-day festival.

Which api are you using?

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.

You can find more info on the AlarmManager docs , and the solution if you still use AlarmManager would be something with setExact or setExactAndAllowWhileIdle .

Be wary when using those, and if you have multiple notifications in a short amount of time, you may consider using a service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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