I have a notification configured for my application and the code is as follows:
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"); }
the application shows a notification, but does not perform any action when you click the buttons on the notification, please help me if something is added to my code.
Update: Since this method is inside the fragment class
I changed part of my code as suggested below, but it did not work:
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; }
Thanks in advance.
source share