如何在用户点击通知时打开特定课程?

时间:2016-09-21 05:34:46

标签: android notifications android-notifications

我的应用包含三个项目的通知,例如还原,重新安排,取消,用户可以选择其中任意三个。当用户选择取消时,通知必须清除。当用户选择还原或重新安排时,必须选择特定类。如何知道通知中按钮点击的后台功能?

以下是 MainActivity 外观

public class MainActivity extends AppCompatActivity {

private String title = "Notification Title";
private String subject = "Notification Subject";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showNotification(getApplicationContext());
}

private void showNotification(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    intent.putExtra("LIST_MAIN_TITLE", title);
    intent.putExtra("LIST_SUB_TITLE", subject);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);
    //  NotificationCompat
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setLights(0xFF0000FF, 100, 3000)
                    .setContentText(subject)
                    .addAction(R.drawable.ic_restore_black_24dp, "Restore", contentIntent)
                    .addAction(R.drawable.ic_schedule_black_24dp, "Re-Schedule", contentIntent)
                    .addAction(R.drawable.ic_cancel_black_24dp, "Cancel", contentIntent);
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}}

以下是通知

enter image description here

1 个答案:

答案 0 :(得分:0)

创建3个单独的待处理意图,每个意图都转到您想要的操作。不要在所有三个按钮上使用相同的按钮。