我已经为我的应用程序设置了一个通知,代码如下:
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/