Android通知.addAction在API 23中已弃用

3npbholx  于 2023-01-24  发布在  Android
关注(0)|答案(6)|浏览(212)

由于addAction(int icon, CharSequence title, PendingIntent intent)已被弃用,在API 23中向通知添加操作的正确方法是什么?找不到任何示例,谢谢。
我的旧操作:.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)

m1m5dgzv

m1m5dgzv1#

而不是这个:
addAction(整型图标、字符序列标题、挂起Intent Intent)

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

use:
addAction(通知.操作操作)
这一切都在开发人员文档!
所以要使用这个:
首先使用NotificationCompat.Action.Builder构建您的操作

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

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

yournotification.addAction(action);
c6ubokkw

c6ubokkw2#

如果API级别〉= 23(marshmallow),则在Drawable的第一个参数处使用Icon
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
示例)

Notification.Action action = new Notification.Action.Builder(
    Icon.createWithResource(this, R.drawable.ic_prev),
    "action string",
    pendingIntent).build();
vtwuwzda

vtwuwzda3#

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

oyt4ldly4#

您只需使用NotificationCompat.Builder构建器来代替Notification.Builder构建器,因为NotificationCompat.Builder允许您在Android 4.1版本以下构建应用程序
使用通知兼容生成器解决:

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());
nwo49xxi

nwo49xxi5#

使用专用生成器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 );
mv1qrgav

mv1qrgav6#

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();

相关问题