Android Multiple Notifications and with multiple intents

前端 未结 4 953
攒了一身酷
攒了一身酷 2021-02-13 19:02

I have a fairly simple app that takes the input from a user and then sets it as a notification. The user can create as many notifications as he/she likes. I want the user to cli

相关标签:
4条回答
  • 2021-02-13 19:37

    You create multiple intents that are mixed. I cleaned up the code (but did not test it)

        NotificationManager nm = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);
    
        Resources res = ctx.getResources();
    
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, ResultActivity.class);
        String pass = text.getText().toString();
        resultIntent.setData(new Uri.Builder().scheme("data")
                .appendQueryParameter("text", "my text").build());
        resultIntent.putExtra("title", pass);
        resultIntent.putExtra("uid", i);
    
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(ResultActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
        builder.setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(
                        BitmapFactory.decodeResource(res,
                                R.drawable.ic_launcher))
                .setTicker("Remember to " + text.getText())
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentTitle(text.getText())
                .setContentIntent(resultPendingIntent);
    
        Notification n = builder.build();
        n.flags = Notification.FLAG_NO_CLEAR;
        nm.notify(i++, n);
    
        text.setText(null);
    
    0 讨论(0)
  • 2021-02-13 19:40

    Use some random requestCode to seperate two notifications

    PendingIntent pendingIntent = PendingIntent.getActivity(context, CommonTools.getRandomNumber(1, 100),
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    public int getRandomNumber(int min, int max) {
        // min (inclusive) and max (exclusive)
        Random r = new Random();
        return r.nextInt(max - min) + min;
    }
    
    0 讨论(0)
  • 2021-02-13 19:42

    I know this was a lot time ago but i feel that the answers have not said anything about the problem in your code. So the problem is pretty much here PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

    So you create a pendingIntent from the stackbuilder whith the flag of update_current. If you look at FLAG_UPDATE_CURRENT it says

     /**
     * Flag indicating that if the described PendingIntent already exists,
     * then keep it but replace its extra data with what is in this new
     * Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>This can be used if you are creating intents where only the
     * extras change, and don't care that any entities that received your
     * previous PendingIntent will be able to launch it with your new
     * extras even if they are not explicitly given to it.
     */
    public static final int FLAG_UPDATE_CURRENT = 1<<27;
    

    So what happens in your use case is that you create two identical pendingintents from the stackbuilder and the second intent overrides the first one . Actually you never create a second you just update the extras of the first one.

    So unfortunately there is no available flag for your use case , but there is a good hack around it. What you can do is use the setAction of your resultIntent and place a random string or a string that makes sense to your app.

    eg. resultIntent.setAction("dummy_action_" + notification.id);

    This will make your resultIntent unique enough , so that the pendingIntent will create it rather than updating a previous one.

    0 讨论(0)
  • 2021-02-13 19:43

    Set different requestCode helps me create and update current intent.

    val pendingIntent = PendingIntent.getActivity(
      this,
      notificationID,
      intent,
      PendingIntent.FLAG_UPDATE_CURRENT
    )
    
    0 讨论(0)
提交回复
热议问题