【问题标题】:Notification with action buttons带有操作按钮的通知
【发布时间】:2017-12-18 01:50:00
【问题描述】:

如何使用 Gmail 应用通知等操作按钮创建通知?

Notification with action buttons sample

【问题讨论】:

标签: android notifications


【解决方案1】:

您可以添加以下代码

 public void createNotification() {
        // Prepare intent which is triggered if the
        // notification is selected
        Intent intent = new Intent(this, NotificationReceiverActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Subject").setSmallIcon(R.drawable.icon)
                .setContentIntent(pIntent)
                .addAction(R.drawable.delete, "Delete", pIntent)
                .addAction(R.drawable.reply, "reply", pIntent)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

    }

【讨论】:

  • 但是这个 addAction 方法在 android 22 及更高版本中可用,我如何在旧版本中处理这个? @Avinash
  • 你可以试试 NotificationCompat.Builder 代替通知
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2016-08-18
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
相关资源
最近更新 更多