通知,我只会收到最后一次通知

时间:2018-06-18 04:52:01

标签: android android-notifications

如果我激活了不同时间运行的更多通知,我将只收到上次日期的通知。如果我一个接一个地激活通知,应用程序可以正常工作(如果我等待第一个通知正在运行,然后我激活下一个通知)。

//NOTIFICATION

long timeInMilliseconds = mDate.getTime();
long time = System.currentTimeMillis();

final int timeToCourse = (int) ((timeInMilliseconds / 1000) - (System.currentTimeMillis() / 1000));

    if(timeToCourse>=0){
            AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

            Calendar calendar=Calendar.getInstance();
            calendar.add(Calendar.SECOND,timeToCourse);

            String formattedDate=targetFormat.format(mDate);

            Intent intent=new Intent("android.media.action.DISPLAY_NOTIFICATION");
            intent.putExtra("first",cours);
            intent.putExtra("second",coursesModel.getPrice());
            intent.putExtra("third",formattedDate);
            PendingIntent broadcast=PendingIntent.getBroadcast(DetailActivity.this,100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);

// AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, MainActivity.class);

    String fCours = intent.getStringExtra("first");
    String fPrice = intent.getStringExtra("second");
    String fDate = intent.getStringExtra("third");

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new Notification.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_s)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.semos_logo_1_round))
            .setColor(Color.BLUE)
            .setContentTitle(fCours)
            .setStyle(new Notification.InboxStyle()
                    .addLine("- " + fPrice)
                    .addLine("-  " + fDate)
            )
            .setContentIntent(pendingIntent).build();


    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
    notificationManager.notify(m, notification);
}}

0 个答案:

没有答案