通知按钮点击不起作用

时间:2016-03-29 06:54:47

标签: android notifications

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

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

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

更新 由于此方法位于片段类

我已根据下面的建议更改了一些代码,但它没有用:

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

提前致谢..

3 个答案:

答案 0 :(得分:0)

您可能需要在此行中将0替换为PendingIntent.FLAG_UPDATE_CURRENT

PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), (int) System.currentTimeMillis(), intent, 0) ;

有关详细信息,请查看this链接。

答案 1 :(得分: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);

.addAction(0,&#34;查看完整提醒&#34;,pi)也像点击通知一样工作。它看起来像一个带有您设置文本的按钮。所以我不会使用,我喜欢保留我的代码。

答案 2 :(得分:0)

尝试以下代码它可能适合您。

Intent intent = new Intent(this, MainActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("App Name")
                    .setContentText(message)
                    .setContentIntent(pIntent)
                    .setSound(soundUri); //This sets the sound to play
            notificationManager.notify(0, mBuilder.build());