Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

java - Firebase: do POST request of received notification

I'm sending notifications through firebase on my Android app and everything works fine, but I would like to use the values from each received notification and POST them to an API (basically storing each notification). Where can I put my POST request so it does the job automatically?

The notifications arrive just fine to the phone in which the app is installed. What I can't do is get their value. In the code I showed above, I have 2 Logs on the OnMessageReceived method, and neither of them print anything, meaning the method probably isn't even called (I think). How can I get the notifications data?

Here's MyFirevaseMessagingService.java (which basically has nothing, as you will see):

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use WorkManager.
                /*scheduleJob();*/
            } else {
                // Handle message within 10 seconds
                handleNow();
            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

    }

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        sendRegistrationToServer(token);
    }

}

Thanks in advance!

question from:https://stackoverflow.com/questions/65864047/firebase-do-post-request-of-received-notification

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to write the received messages to a cloud API, you should call that API inside the onMessageReceived method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...