通知操作单击时触发功能

时间:2017-09-05 11:13:20

标签: android notifications kotlin android-notifications notification-action

我正在Kotlin中编写一个应用程序,该应用程序应显示带有操作按钮的通知。

我上课了。我们称之为NotificationClass

此课程通过fun createNotification()

为我创建通知

一切正常。但是我想在通知中添加一个按钮,当点击该按钮时,会在我的NotificationClass中触发一个名为notificationActionClicked()的功能。

我有办法做到这一点吗?

(通知类不是Android服务。它只是一个实用程序类。)

1 个答案:

答案 0 :(得分:0)

您需要查看待处理的意图

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

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

    notificationManager.notify(0, noti);

}