广播接收器会覆盖以前的通知

我已经使该接收器的通知显示得很好,只有仅显示最后一个通知,其他通知才被覆盖,如何避免?

public class MyBroadCastReceiver  extends BroadcastReceiver {

    String TAG="onReceiveBroadcasst";

    public  MyBroadCastReceiver() {

    }

    Context context;
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onReceive(Context context,Intent intent) {
        Log.i(TAG,"onReceive: "+constants.notification);
        this.context=context;
        notification_maker("Running Late.","Hurry Up,Time to leave.",BitmapFactory.decodeResource(context.getResources(),R.drawable.late));
        notification_maker("Forecast.","Today's foreacast",R.drawable.late));
        notification_maker("Notification.",R.drawable.late));

        city=new Prefmanager(context).getStringPref("City",city);
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public  void notification_maker(String Title,String notification,Bitmap bitmap) {
        Bitmap icon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.cloudy);
        NotificationCompat.BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle();
        bigPicture.bigPicture(icon1);

        ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_NOTIFICATION,100);
        toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP,1000);
        Notification notif = new Notification.Builder(context)
                .setContentTitle(Title)
                .setContentText(notification)
                .setSmallIcon(R.drawable.cloudy)
                .setLargeIcon(bitmap)
                .setStyle(new Notification.BigTextStyle().bigText(notification)
                        )
                .build();
        Notificationmanager notificationmanager=(Notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationmanager.notify(2,notif);
        Log.i(TAG,Title+"notification_maker: "+notification);
    }
}

以下代码将仅显示最后一个通知,应用日志中有其他通知被成功调用的日志,如何避免?

pantial 回答:广播接收器会覆盖以前的通知

由于所有通知都使用相同的ID,因此旧通知已被覆盖:

notificationManager.notify(2,notif);

使用不同的ID区分通知(即,不要对所有通知使用2

本文链接:https://www.f2er.com/3167253.html

大家都在问