Notification Buttons do not work

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.

+5
source share
3 answers

You probably need to replace 0 with PendingIntent.FLAG_UPDATE_CURRENT in this line

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

For more information check out this link.

0
source

this is what i use in my application.

 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) also works like clicking on a notification. it is displayed as a button with the given text. therefore I do not use, I like to save my codes.

0
source

Try the following code. It may be work for you.

 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()); 
0
source

Source: https://habr.com/ru/post/1245962/


All Articles