1

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

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

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

这一切都很好。但是我想在通知中添加一个按钮,单击该按钮会触发我NotificationClass被调用的函数notificationActionClicked()

有没有办法我可以做到这一点?

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

4

1 回答 1

1

您需要查看未决意图

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

}
于 2017-09-05T11:18:51.860 回答