在api 23中弃用了Android通知.addAction

时间:2016-02-26 09:23:07

标签: android

由于API 23已被弃用,在addAction(int icon, CharSequence title, PendingIntent intent)中向通知添加操作的正确方法是什么?无法找到任何例子,谢谢。

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

4 个答案:

答案 0 :(得分:17)

而不是这一个:

  

addAction (int icon, CharSequence title, PendingIntent intent)

     

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

使用:

  

addAction (Notification.Action action)

这一切都在开发者文档中!

所以要使用它:

首先使用NotificationCompat.Action.Builder

构建您的操作
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build();

注意:使用NotificationCompat.Action

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

yournotification.addAction(action);

答案 1 :(得分:10)

在第一个参数中使用Icon类为Drawable 如果api级别> = 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

例)

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

答案 2 :(得分:2)

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

答案 3 :(得分:1)

您只需使用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());