通知按钮点击不起作用

sam*_*pta 6 notifications android

我为我的应用设置了通知,代码如下:

public int getNotification( View view) {

        NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(getActivity() ,RouteMap.class );
        intent.putExtra("Stop Ride",true);
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), (int) System.currentTimeMillis(), intent, 0) ;

        if (Build.VERSION.SDK_INT < 16) {
            getToast("API below 16 ...notification not supported");

        } else {

            Notification notify = new Notification.Builder(getActivity())
                    .setSmallIcon(R.drawable.icon1)
                    .setContentTitle("Car Rental")
                    .setContentText("Click to view ")
                    .setOngoing(true)
                    .setContentIntent(pendingIntent)
                    .addAction(R.drawable.icon3,"View ",pendingIntent)
                    .addAction(R.drawable.alert_icon,"Stop Ride", pendingIntent )
                    .build();

            notificationManager.notify(notificationID, notify);

        }

        return notificationID;
    }

    public void removeNotification() {
        NotificationManager manager = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(
                notificationID);
        getToast("Notification Removed");
    }
Run Code Online (Sandbox Code Playgroud)

应用程序显示通知但在点击通知按钮时不执行任何操作,如果要将某些内容添加到我的代码中,请帮助我.

更新: 因为此方法位于片段类中

我已经改变了我的一些代码,如下所示,但它没有用:

 public int getNotification( View view) {

   // NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(getActivity(), RouteMap.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    if (Build.VERSION.SDK_INT < 16) {
        getToast("API below 16 ...notification not supported");

    } else {

        Notification notify = new Notification.Builder(getActivity())
                .setSmallIcon(R.drawable.icon12)
                .setContentTitle("Car Rental")
                .setContentText("Click to view ")
                .setOngoing(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.icon3, "View ", pendingIntent)
                .addAction(R.drawable.alert_icon, "Stop Ride", pendingIntent)
                .build();

        notificationManager.notify(notificationID, notify);

    }

    return notificationID;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢..

rma*_*alo 0

这就是我在我的应用程序中使用的。

Intent i = new Intent(ctx, NotifReceiver.class);

        PendingIntent pi = PendingIntent.getActivity(ctx, notifNum, i, 0);

        Notification notif = new Notification.Builder(ctx)
                .setContentTitle("MyPower Reminder")

                .setContentText(contentTxt)
                .setSmallIcon(R.drawable.ic_mypower4)
                .setContentIntent(pi)
                .setSound(notifSound)

                //.addAction(0, "View Full Reminder", pi)
                .build();

        NotificationManager notifMgr = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notifMgr.notify(0, notif);
Run Code Online (Sandbox Code Playgroud)

.addAction(0, "View Full Reminder", pi) 的作用也类似于单击通知。它看起来像一个带有您设置的文本的按钮。所以我不使用,我喜欢保留我的代码。