如何将纬度和纵向从 (FCM) 推送通知传递到 Android 活动



**我只想将纬度和纵向作为通知传递,然后单击它,在该位置打开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);
}
}

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

您可以打开 Google 地图,通过将纬度/液化天然气作为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调用它,如本答案中所述。

相关内容

  • 没有找到相关文章

最新更新