广播接收器覆盖以前的通知
Broadcast Receiver overwrites previous notifications
我有这个接收器通知显示正常只是只显示最后一个通知其他被覆盖,如何避免它?
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",BitmapFactory.decodeResource(context.getResources(), R.drawable.late));
notification_maker("Notification.","Hurry Up, Time to leave.",BitmapFactory.decodeResource(context.getResources(), 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);
}
}
以下代码只会显示最后一条通知,App-logs中有其他通知调用成功的记录,如何避免?
旧通知正在被覆盖,因为您对所有通知使用相同的 ID:
notificationManager.notify(2,notif);
使用不同的 ID 来区分通知(即不要对所有通知使用 2
)
我有这个接收器通知显示正常只是只显示最后一个通知其他被覆盖,如何避免它?
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",BitmapFactory.decodeResource(context.getResources(), R.drawable.late));
notification_maker("Notification.","Hurry Up, Time to leave.",BitmapFactory.decodeResource(context.getResources(), 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);
}
}
以下代码只会显示最后一条通知,App-logs中有其他通知调用成功的记录,如何避免?
旧通知正在被覆盖,因为您对所有通知使用相同的 ID:
notificationManager.notify(2,notif);
使用不同的 ID 来区分通知(即不要对所有通知使用 2
)