Android notification with buttons on it

前端 未结 6 625
暖寄归人
暖寄归人 2020-11-30 02:06

I\'m trying to make a notification with 2 buttons on it:

  • one takes me back to the activity
  • the other closes it

Has anyone got an idea o

6条回答
  •  無奈伤痛
    2020-11-30 02:45

    If you want to assign specific intent to a button:

    views.setOnClickPendingIntent(R.id.your_button_id, pendingIntent);
    

    I suppose that you need only one intent to be sent when the button is clicked, so you have to AVOID setting the main notification intent

    notification.contentIntent = yourPendingIntent;
    

    Otherwise (if you set "notification.contentIntent = pendingIntent;" as usual) both intents will be called which might not be what you want/user expects.

    If you still want pressing other parts of the notification invoke that general intent (or any other) you can use the same method of intent-per-view assignment as above. Don't forget to set

    android:clickable="true"
    

    to any view you'd like to track onClick() for.

    You can track these intents by their extras in the activity they are calling. If you are calling you main/launcher activity than you'll track them here (as it comes from javadoc for this method):

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle data = intent.getExtras();
    
        if (data != null && data.containsKey(YOUR_INTENT_KEY_SOURCE_CONSTANT)) {
           // process your notification intent
        }
    
        // go on with smth else
    }
    

提交回复
热议问题