簡體   English   中英

當應用程序在android中處於后台時如何從推送通知中接收自定義數據

[英]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());

}

當應用程序在后台時,此方法永不調用,因此當應用程序在后台時,我無法接收任何數據

任何幫助將不勝感激...

您應該在服務中實現onMessageReceived()。 因此,當您的應用程序在后台運行時,它仍然會收到正常的回調。 Firebase支持所有這些。

在AndroidManifest.xml中聲明

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

在Java代碼中:

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);
}

發送數據時,添加這兩個屬性'ttl' => 3600, 'content_available' => true

例如

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

這對我有幫助。

那么這應該工作

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());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM