android - 通知按钮点击不起作用

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

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

更新: 由于此方法在 fragment 类中

我已经按照下面的建议更改了一些代码,但是没有用:

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

提前致谢..

最佳答案

这是我在我的应用中使用的。

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, "View Full Reminder", pi) 也像点击通知一样工作。它看起来像一个带有您设置的文本的按钮。所以我不使用,我喜欢保留我的代码。

关于android - 通知按钮点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36277402/

相关文章:

iOS 通知触发器 : fortnightly and/or quarterly

android - Android 中的空游标

Android 可调整工具栏大小以相应地在 Observable ScrollView 上滚动

C++ 的 Java 等价物 +'s "std::string::find_first_of"

android - 未找到 selectableItemBackground

google-apps-script - 如果Google脚本的 “My Executions”显示脚本未在指定时间内运行,如何获取通知

notifications - Laravel 更改通知表的连接

android - React native 地理定位不适用于设备

android - Eclipse 的 Android SDK 文件夹结构中的 my\sdk\extras\android\m2repository\folder 在哪里?

push-notification - 通知分组在 Android 8.1 中不起作用