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 –

相關問題