在API 23中,Android通知中的.addAction已经被弃用。

17

API 23中,由于addAction(int icon, CharSequence title, PendingIntent intent)已被弃用,因此添加通知动作的正确方法是什么?我找不到任何示例,请谢谢。

我的旧动作:.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)


根据文档,你应该使用addAction(Notification.Action action) - Tigger
https://dev59.com/5mQn5IYBdhLWcg3wamjO - sasikumar
3
我已经查看了@sasikumar提供的那个例子,但它对我没有帮助,我不明白如何构建Notification.Action action - Marian Pavel
6个回答

25

使用以下方法替换原有的方法:

addAction (Notification.Action action)

而不是这个:

addAction (int icon, CharSequence title, PendingIntent intent)

此方法已在API级别23中弃用。

所有内容均在开发人员文档中!

因此,要使用此功能:

首先使用NotificationCompat.Action.Builder构建您的操作。

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

注意:使用NotificationCompat.Action

然后将其添加到您的通知中:

yournotification.addAction(action);

2
我知道,我已经阅读过了,但我不理解如何正确构建 Notification.Action action - Marian Pavel
2
这是不正确的,类型不兼容,你需要将 new Notification.Action.Builder() 改为 new Action(),结果:new Notification.Action(.....) 仍然已被弃用或者反之亦然。 - Marian Pavel
为了使其工作,请使用“new NotificationCompat.Builder(context)” - TouchBoarder
Notification.Action 似乎并没有被弃用。 - ben_joseph
你好,我们能否在添加操作中添加彩色图标?因为它会将彩色图标变成灰色。 - Awadesh

16

旧问题,但仍需要它 :) 今天要测试它,我会回答的,谢谢。 - Marian Pavel

4
//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);
}

2

您只需使用NotificationCompat.Builder构建器代替Notification.Builder构建器,因为NotificationCompat.Builder允许您在Android 4.1以下版本中构建应用程序

通过使用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());

1
这对我非常有效。安卓有这么多创建愚蠢通知的方式似乎有些冗余? - Thunderstick

1
Actually, the only option to use all non-deprecated methods is by Using Icon for Action.Builder.

    
Notification.Action action = new Notification.Action.Builder(Icon.createWithResource(this, R.drawable.act_hike), 
getString(R.string.finish_recording), pActionFinish).build();

//Create a notification
Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.atlas)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.foreground_notification))
.addAction(action)
.setContentIntent(pIntent)
.build();

0

使用特殊的构建器Notification.Action.Builder

    Notification.Action.Builder builder = new Notification.Action.Builder( Icon.createWithResource( this, R.drawable.ic_pause), getString( R.string.pause ), PendingIntent.getBroadcast( this, 1, new Intent( TIMER_PAUSE ), 0 ) );
    Notification.Action action = builder.build();
...
    notification_builder.addAction( action ); // or setActions( action );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接