点击通知动作的触发功能

我正在写一个应用程序在Kotlin应该显示一个通知与一个行动按钮。

我有一堂课。 我们称之为NotificationClass

这个类通过fun createNotification()为我创建通知

这一切工作正常。 但是我想为通知添加一个按钮,点击的时候会触发一个名为notificationActionClicked() NotificationClass函数。

有什么办法可以做到这一点?

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

你需要看看待定的意图

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