如何将(FCM)推送通知中的纬度和经度传递到Android活动

时间:2017-05-25 17:03:37

标签: android google-maps firebase android-intent firebase-cloud-messaging

**我想做的就是将纬度和经度作为通知传递,然后点击它,在该位置打开Android Google地图。我已阅读了很多帖子,但我无法弄明白,如果我必须将坐标作为URL或其他东西传递给我的应用程序活动**

要在点击推送通知时打开活动(SomeActivity)(使用CLICK_ACTION),我使用Postman。

{
  "to": 
    "/topics/NEWS"
  ,
  "data": {
    "extra_information": "TestProject"
  },
  "notification": {
    "title": "NEW INCIDENT",
    "text":  "Opening Google Maps",
    "click_action": "SOMEACTIVITY"
  }
}

Java文件是:

package com...;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by User on 2/23/2017.
 */


public class SomeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.some_activity_layout);
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=" + "40.589352" + "," + "23.030262"));
        startActivity(intent);
    }
}

您的帮助将不胜感激,绝对会让我的一天成功!

1 个答案:

答案 0 :(得分:1)

您可以通过将lat / lng作为data而非notification的属性发送,打开Goog​​le地图并在某个位置显示标记。例如:

{
  "to": 
    "/topics/NEWS"
  ,
  "data": {
    "title": "NEW INCIDENT",
    "lat": "37.8726483",
    "lng": "-122.2580119"
  }
}

然后,在您的消息服务中,获取数据并自己生成通知:

public class MessagingService extends FirebaseMessagingService {
    private static final String TAG = "MessagingService";

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

        Map<String, String> msgData = msg.getData();
        Log.i(TAG, "onMessageReceived: " + msgData);

        if (msgData != null) {
            postNotification(msgData.get("title"), msgData.get("lat"), msgData.get("lng"));
        }
    }

    private void postNotification(String title, String lat, String lng) {
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                Uri.parse("http://maps.google.com/maps?q=loc:" + lat + "," + lng));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setCategory(NotificationCompat.CATEGORY_STATUS)
                        .setContentInfo(lat + '/' + lng)
                        .setContentIntent(pendIntent)
                        .setContentTitle(title)
                        .setSmallIcon(R.mipmap.ic_launcher);

        NotificationManager mgr =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mgr.notify(1, builder.build());
    }
}

如果您编写了自己的使用MapFragment显示Google地图的活动,则可以click_action中的说明使用double t = (int)(Math.random() * 7 + 1); double o; do { o = (int)(Math.random() * 6 + 1); } while(o == t); 调用该地图。