我一直在尝试使用Firebase云消息传递和凌空从纬度和经度将位置数据从一台设备发送到另一台设备。我正在接收通知,但是我尝试发送的其他内容没有收到。
private void sendNotification(final String key, final Double latitude, final Double longitude) {
Toast.makeText(this,""+latitude+" "+longitude,Toast.LENGTH_SHORT).show();
String notificationChannelId = "DistressSignalAlert";
String Lat = String.valueOf(latitude);
String Long = String.valueOf(longitude);
JSONObject mainObj = new JSONObject();
try {
mainObj.put("to", "/topics/" + key);
JSONObject notificationObject = new JSONObject();
notificationObject.put("title", "Emergency Alert");
notificationObject.put("body", "This person is in danger help her out :" +latitude+"/ "+longitude);
JSONObject locationData = new JSONObject();
locationData.put("Latitude",Lat);
locationData.put("Longitude",Long);
mainObj.put("notification", notificationObject);
mainObj.put("extraData",locationData);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL,
mainObj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "" + key + " " + latitude + " " + longitude, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("content-type", "application/json");
header.put("authorization", "key=");
return header;
}
}
这是我发送通知的代码。
我的用于接收通知并创建未决意图的代码是
public void onMessageReceived(@NonNull RemoteMessage remoteMessage){
super.onMessageReceived(remoteMessage);
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Map<String,String> extraData = remoteMessage.getData();
String DestinationLatitude = extraData.get("Latitude");
String DestinationLongitude = extraData.get("Longitude");
String notificationChannelID = "DistressSignalAlert";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,notificationChannelID)
.setContentTitle(title)
.setContentText(body)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.login_logo))
.setSmallIcon(R.drawable.login_logo);
Bundle bundle = new Bundle();
bundle.putString("DestinationLatitude",DestinationLatitude);
bundle.putString("DestinationLongitude",DestinationLongitude);
Intent intent = new Intent(this,User_Activity.class);
intent.putExtra("Bundle",bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,10,intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelID,"distressSignal",NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(NOTIFICATION_ID,builder.build());
}
我已经在我的活动的onCreate()中编写了此代码
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String destinationLatitude = extras.getString("destinationLatitude");
String destinationLongitude = extras.getString("destinationLongitude");
Toast.makeText(this,""+destinationLatitude+" "+destinationLongitude,Toast.LENGTH_SHORT).show();
createDistressSignalLocationOnMap(destinationLatitude,destinationLongitude);
}
您在putString中使用的“ DestinationLatitude”
bundle.putString(“ DestinationLatitude”,DestinationLatitude);
和您使用过的getString “ destinationLatitude”
String destinationLatitude = extras.getString(“ destinationLatitude”);
[答案您拼写错误,在上方使用大写字母,在下方使用小写字母。