【问题标题】:How to pass Latitude and Longtitude from (FCM) push notification to an Android activity如何将纬度和经度从(FCM)推送通知传递到 Android 活动
【发布时间】:2017-05-25 17:03:36
【问题描述】:

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

要在单击我的推送通知时打开活动 (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);
    }
}

您的帮助将不胜感激,一定会让我开心!!!

【问题讨论】:

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


    【解决方案1】:

    您可以通过将纬度/经度作为data 而不是notification 的属性发送来打开谷歌地图并在位置上显示标记。例如:

    {
      "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 调用它,如this answer 中所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多