如何让程序在屏幕上显示多个通知?

如何让程序在屏幕上显示多个通知?

问题描述:

我似乎不明白如何显示多个通知没有一个叠加另一个。在我的情况下,它只在当时显示一个。 picture 1如何让程序在屏幕上显示多个通知?

我的目标是得到它的工作,像下面 picture 2

我应该改变或可能添加到我的代码截图?分配通知

@Override 
    protected void onPostExecute(Bitmap result) { 

     super.onPostExecute(result); 
     Intent intent = new Intent(ctx, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra("com.example.romanchuk.appisode.notifyId", id); 
     intent.putExtra("com.example.romanchuk.appisode.show_id", show_id); 
     PendingIntent pendingIntent = PendingIntent.getActivity(ctx, sNotificationId /* Request code */, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

     inboxStyle.addLine(message); 

     NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); 
     bigText.bigText(message); 
     bigText.setBigContentTitle(getString(R.string.app_name)); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx); 
     Notification notification = null; 
     notification = builder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0) 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) 
       .setColor(getResources().getColor(R.color.color_accent)) 
       .setContentTitle("Appisode") 
       .setContentIntent(pendingIntent) 
       .setFullScreenIntent(pendingIntent, true) 
       .setContentText(message) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setAutoCancel(true) 
       .setStyle(inboxStyle) 
       .setSmallIcon(R.drawable.small_icon) 
       .setSound(defaultSoundUri).build(); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(sNotificationId++, notification); 
    } 
+0

什么是你的** sNotificationId **这是不变的..? –

+0

这是可变的 private static int sNotificationId = 1; – JustNice

+0

检查我的答案,如果你想要多个通知,它应该是不同的通知。 –

如果你想显示多个通知,通知ID应该是不同的,如果通知ID在通知中已经存在,它将覆盖该通知

块的代码。

notificationManager.notify(sNotificationId++, notification); 

在这种sNotificationId应该为所有通知不同

如果您idshow_id为int而不是恒定的,如果它会为每个通知不同,你可以使用也可以作为通知ID。

还是尽量给不同的标签进行这样每个通知,

notificationManager.notify(String.valueOf(System.currentTimeMillis()), sNotificationId++, notification);