【问题标题】:Android notification .addAction deprecated in api 23Android 通知 .addAction 在 api 23 中已弃用
【发布时间】:2021-11-26 16:51:53
【问题描述】:

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

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

【问题讨论】:

  • 根据docs,你应该改用addAction (Notification.Action action)
  • 我已经看过那个例子@sasikumar,它对我没有帮助,我不明白如何构建Notification.Action action

标签: android


【解决方案1】:

而不是这个:

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

【讨论】:

  • 我知道,我已经读过了,但我不明白如何正确构建Notification.Action action
  • 这是不正确的、不兼容的类型,您需要将new Notification.Action.Builder() 更改为new Action(),结果:new Notification.Action(.....) 仍然被弃用,反之亦然,仍然被弃用。
  • 为此使用'new NotificationCompat.Builder(context)'
  • Notification.Action 似乎没有被弃用。
  • 嗨,我们可以在添加动作中添加彩色图标吗,因为它会将彩色图标变成灰色
【解决方案2】:

在 Drawable 的第一个参数处使用 Icon如果 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

示例)

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

【讨论】:

  • 老问题,但仍然需要它:) 今天去测试它,我会给出答案,谢谢。
【解决方案3】:
//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);
}

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 这对我很有用。安卓有这么多方法来创建愚蠢的通知似乎是多余的?
    【解决方案5】:

    使用特殊的构建器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 );
    

    【讨论】:

      猜你喜欢
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多