0

I need to set a onclicklistener on a notification, so that I can call a PHP file, and get data from my database, and then set it into the activity that is intended by my notification, can you please give me some clues?
I looked up in other questions but I didn't really understand how to do that. Here is my code when I set up the notification:

public class FirebaseMessagingService extends 
com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {

    Intent i = new Intent(this, ActivityRestaurantHistoryReservations.class);

    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("You have a new reservation !")
            .setContentText(message)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0, builder.build());
   }
}

Thank you for your help

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Lie Hili
  • 33
  • 10

2 Answers2

1

i need to set a onclicklistener on a notification

That is not possibly directly.

so that i can call a PHP file, and get data from my database, and then set it into the activity that is intented by my notification

When the user taps on your notification, the user will be taken to the ActivityRestaurantHistoryReservations activity, using your existing code. So, either:

  • Put your "call a PHP file" and related logic in ActivityRestaurantHistoryReservations, or

  • Change the PendingIntent to something else that will perform this work, or

  • Add a separate action (addAction()) that puts another button in the notification, and have its PendingIntent go to something else that will perform this work (in which case, this work will only be performed if the user clicks on that action button specifically)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I recommend you to check this Add list item in RecyclerView Adapter on receiving Firebase message article out to understand how to send data from FirebaseMessagingService to Activity

Community
  • 1
  • 1
Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52