简体   繁体   中英

how to receive custom data from push notification when app is in background in android

 @Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

    if (remoteMessage.getData().size() > 0)
    {
        Log.e(TAG, "Message body:" + remoteMessage.getNotification().getBody());

        for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet())
        {
            String key = entry.getKey();
            String value = entry.getValue();
            data.add(value);
            Log.e(TAG, "key, " + key + " value " + value);

        }

        post_id = data.get(0);
        lang = data.get(1);
        link = data.get(2);

        Log.e("post_id",post_id);
        Log.e("lang",lang);
        Log.e("link",link);
    }

    sendNotification(remoteMessage.getNotification().getBody());

}

when app is in background this method never called and therefore i cant receive any data when app is in background

Any help will be appreciated...

You should implement onMessageReceived() in service. So when your application in background, it's still receive this call back normal. Firebase supported all of this.

in AndroidManifest.xml declare

    <service android:name=".services.MyFirebaseMessagingService">
       <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT" />
       </intent-filter>
    </service>

in Java code:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //do receive data
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getData() != null)
        sendNotification(remoteMessage);
}

when sending the data add these two attributes 'ttl' => 3600, 'content_available' => true

eg

return Curl::to('https://fcm.googleapis.com/fcm/send')
            ->withHeader('Authorization: key=' . env('FIREBASE_KEY'))
            ->withHeader('Content-Type: application/json')
            ->withData(['to' => $firebase_token, 'data' => $data, 'ttl' => 3600,  'content_available' => true ])
            ->asJson(true)
            ->post();

this helped me before.

then this should work

int someArg = Integer.parseInt(remoteMessage.getData().get(AdminCommands.KEY_SOME_ARG ));
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }

    }

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

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}




private void handleDataMessage(JSONObject json) {
    Log.e(TAG, "push json: " + json.toString());
    String message = "";
    String notification_type = "";
    String name = "";
    int id = 0;
    String title = "";


    JSONObject postData = null;
    int notify_id = 0;


    try {

        JSONObject data = json.getJSONObject("notification");

            message = data.getString("body");


        if (data.getJSONObject("obj") != null) {
            postData = data.getJSONObject("obj");
            message = postData.getString("message");
            notification_type = postData.getString("notification_type");
            id = postData.getJSONObject("response").getInt("id");
            name = postData.getJSONObject("response").getString("name");


        }
            title = data.getString("title");


        Log.e(TAG, "title: " + title);
        Log.e(TAG, "postData: " + postData);





    } catch (JSONException e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM