Android notification .addAction deprecated in api 23

后端 未结 4 1975
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 12:45

What is the proper way to add an action to the notification in API 23 since addAction(int icon, CharSequence title, PendingIntent intent) is deprec

相关标签:
4条回答
  • 2020-12-01 13:22

    Use Icon class at first parameter for Drawable if api level >= 23(marshmallow)

    https://developer.android.com/reference/android/app/Notification.Action.Builder.html

    https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.Action.Builder.html

    example)

    Notification.Action action = new Notification.Action.Builder(
        Icon.createWithResource(this, R.drawable.ic_prev),
        "action string",
        pendingIntent).build();
    
    0 讨论(0)
  • 2020-12-01 13:23
    //Exemple of notification with Button
    private void scheduleNotificationWithButton(String message) {
    
        int notifReCode = 1;
    
        //What happen when you will click on button
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
    
        //Button
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();
    
        //Notification
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("Back to Application ?")
                .setContentTitle("Amazing news")
                .addAction(action) //add buton
                .build();
    
        //Send notification
        NotificationManager notificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }
    
    0 讨论(0)
  • 2020-12-01 13:24

    Instead of this one:

    addAction (int icon, CharSequence title, PendingIntent intent)

    This method was deprecated in API level 23.

    Use:

    addAction (Notification.Action action)

    It's all in the developer docs!

    So to use this:

    first build your action with the NotificationCompat.Action.Builder

    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build();
    

    Note: Use NotificationCompat.Action

    And than add it to your notification:

    yournotification.addAction(action);
    
    0 讨论(0)
  • 2020-12-01 13:39

    You just have to use NotificationCompat.Builder builder in place of Notification.Builder builder because NotificationCompat.Builder allows you to build your application below Android Version 4.1

    Solved by using NotificationCompat.builder:

        String strTitle = getString(R.string.new_message);
        String strText = getString(R.string.hi_whats_up);
        Intent intent = new Intent(this, NotificationView.class);
        intent.putExtra("title", strTitle);
        intent.putExtra("text", strText);
    
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setTicker("Notification Ticker")
               .setContentTitle("Notification Title")
               .setContentText("Notification Text")
               .addAction(R.mipmap.ic_launcher, "Notification Action", pendingIntent)
               .setContentIntent(pendingIntent)
               .setAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               notificationManager.notify(0, builder.build());
    
    0 讨论(0)
提交回复
热议问题