How to display multiple notifications in android

前端 未结 18 1259
既然无缘
既然无缘 2020-11-29 19:39

I am receiving only one notification and if there comes another notification, it replaces the previous one and here is my code

private static void generateNo         


        
相关标签:
18条回答
  • 2020-11-29 20:09

    Replace your line with this.

    notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
    
    0 讨论(0)
  • 2020-11-29 20:10

    i guess this will help someone..
    in below code "not_nu" is an random int.. PendingIntent and Notification have the same ID .. so that on each notification click the intent will direct to different activity..

    private void sendNotification(String message,String title,JSONObject extras) throws JSONException {
       String id = extras.getString("actionParam");
        Log.e("gcm","id  = "+id);
        Intent intent = new Intent(this, OrderDetailActivty.class);
        intent.putExtra("id", id);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        final int not_nu=generateRandom();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, not_nu /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_cart_red)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(not_nu /* ID of notification */, notificationBuilder.build());
    }
    public int generateRandom(){
        Random random = new Random();
        return random.nextInt(9999 - 1000) + 1000;
    }
    
    0 讨论(0)
  • 2020-11-29 20:12

    Use the following method in your code.

    Method call :-

    notificationManager.notify(getCurrentNotificationId(getApplicationContext()), notification);
    

    Method:-

      *Returns a unique notification id.
             */
    
            public static int getCurrentNotificationId(Context iContext){
    
                NOTIFICATION_ID_UPPER_LIMIT = 30000; // Arbitrary number.
    
                NOTIFICATION_ID_LOWER_LIMIT = 0;
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(iContext);
            int previousTokenId= sharedPreferences.getInt("currentNotificationTokenId", 0);
    
            int currentTokenId= previousTokenId+1;
    
            SharedPreferences.Editor editor= sharedPreferences.edit();
    
            if(currentTokenId<NOTIFICATION_ID_UPPER_LIMIT) {
    
                editor.putInt("currentNotificationTokenId", currentTokenId); // }
            }else{
                //If reaches the limit reset to lower limit..
                editor.putInt("currentNotificationTokenId", NOTIFICATION_ID_LOWER_LIMIT);
            }
    
            editor.commit();
    
            return currentTokenId;
        }
    
    0 讨论(0)
  • 2020-11-29 20:14

    The problem is with your notificationId. Think it as an array index. Each time you update your notification, the notificationId is the place it takes to store value. As you are not incrementing your int value (in this case, your notificationId), this always replaces the previous one. The best solution I guess is to increment it just after you update a notification. And if you want to keep it persistent, then you can store the value of your notificationId in sharedPreferences. Whenever you come back, you can just grab the last integer value (notificationId stored in sharedPreferences) and use it.

    0 讨论(0)
  • declare class member
    static int i = 0;
    
    mNotificationManager.notify(++i, mBuilder.build());
    
    0 讨论(0)
  • 2020-11-29 20:17

    Simple notification_id needs to be changable.

    Just create random number for notification_id.

        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
    

    or you can use this method for creating random number as told by tieorange (this will never get repeated):

        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
    

    and replace this line to add parameter for notification id as to generate random number

        notificationManager.notify(m, notification);
    
    0 讨论(0)
提交回复
热议问题