0

I have tried to add a click event onto the push notification but I get an error every time. Without the click event, it works fine. It is showing the notification perfectly on time. I have tried to do this with an Intent.

Is this the way to do it?
How do I implement a click event on the notification itself?

Here is what I currently have:

public class MyFirebaseInstanceService  extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if (remoteMessage.getData().isEmpty()){
            showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
        }else {
            showNotification(remoteMessage.getData());
        }

    }
    private  void  showNotification(Map<String,String> data){
        String title=data.get("title").toString();
        String body=data.get("body").toString();

        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        String NOTIFICATION_CHANNEL_ID="example.mfree.services.test";

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel=new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
                        NotificationManager.IMPORTANCE_DEFAULT);

                notificationChannel.setDescription("Dipu");
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.BLUE);
                notificationChannel.enableLights(true);
                notificationManager.createNotificationChannel(notificationChannel);
            }

        NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
            notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).
                    setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_money)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setContentInfo("Info");
            notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
    }

    private  void showNotification(String title,String body){

        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        String NOTIFICATION_CHANNEL_ID="com.example.mfree.services.test";

        Intent intent = new Intent(getApplicationContext(), Notification_send_Activity.class);
        startActivity(intent);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel=new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setDescription("Dipu");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.enableLights(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).
                setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_money)
                .setContentTitle(title)
                .setContentText(body)
                .setContentInfo("Info");
        notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
    }

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);

        Log.d("TOKENFIREBASE",s);
    }
}
F. Müller
  • 3,969
  • 8
  • 38
  • 49
  • What is the error? Please add that to your question. – F. Müller Jul 15 '20 at 19:24
  • Could you explain a little bit more on how you added the "click event"? Also, what do you really want to do by clicking the notification? Take note that notification can only accept `PendingIntent` as a click action. Please also read [Set the notification's tap action](https://developer.android.com/training/notify-user/build-notification#builder) – Andrew T. Jul 15 '20 at 19:38

1 Answers1

0

You need to add an action

Notification.Action action = new NotificationCompat.Action(icon, title, pendingIntent);

Notification notification = new NotificationCompat.Builder(context)
   .addAction(action)
   .build();