0

**我想要做的就是将纬度和经度作为通知传递,并通过单击它来打开该位置上的Android Google地图。我已经阅读了很多文章,但我无法弄清楚,如果我必须通过URL或其他东西传递到我的应用程序活动**如何将(FCM)推送通知的纬度和经度传递给Android活动

当我的推送通知是打开活动(SomeActivity)点击(使用CLICK_ACTION),我使用邮递员。

{ 
    "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

您可以打开Goog​​le地图,并通过发送纬度/长度为data的属性而不是notification来在位置上显示标记。例如:

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

然后,在你的消息服务,获取数据并生成通知自己:如果你写你自己的活动,使用MapFragment显示谷歌地图

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

,然后您可以使用click_action来调用它,如this answer中所述。

+0

**谢谢!!! ** Bob Snyder –

相关问题