Как получить дополнительные данные из уведомления pu sh, отправленного с одного устройства на другое в android? - PullRequest
0 голосов
/ 20 января 2020

Я пытался отправить данные о местоположении по широте и долготе с одного устройства на другое, используя облачные сообщения и залп 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);
    }

1 Ответ

2 голосов
/ 20 января 2020

вы использовали в putString "DestinationLatitude"

bundle.putString ("DestinationLatitude", DestinationLatitude);

и getString, которые вы использовали "destinationLatitude"

String destinationLatitude = extras.getString ("destinationLatitude");

Ответ вы написали неправильное написание, выше вы использовали первую букву заглавную, а ниже вы использовали маленькую букву .

...