如何在 android 中从一台设备发送到另一台设备的推送通知中获取额外数据?
How do i fetch extra data from a push notification sent from one device to another in android?
我一直在尝试使用 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());
}
我已经在 activity
的 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",目的地纬度);
和您使用的 getString "destinationLatitude"
String destinationLatitude = extras.getString("destinationLatitude");
回答你写错了,上面你用大写字母,下面你用小写字母。
我一直在尝试使用 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());
}
我已经在 activity
的 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",目的地纬度);
和您使用的 getString "destinationLatitude"
String destinationLatitude = extras.getString("destinationLatitude");
回答你写错了,上面你用大写字母,下面你用小写字母。