我知道关于这个主题有几个问题,但我很绝望......
我正在尝试将可序列化对象发送到 Android 手机。
当应用程序打开时,通知工作正常,但是当在后台时,会出现通知,但不能正确处理它,只显示标题。
我发送的 json 是:
{
"to":"eSKuqqNvN_dkM71eJzrulCzgKn",
"body":{
"mZona":"V",
"mProvinciaCliente":"Segovia",
"mScrapie":false,
"mBrucelosis":false,
"mTuberculosis":false,
"mOtrasEnfermedades":false,
"latitud":0.0,
"longitud":0.0
},
"notification":{
"title":"nuevos"
},
"data":{
"mZona":"V",
"mProvinciaCliente":"Segovia",
"mScrapie":false,
"mBrucelosis":false,
"mTuberculosis":false,
"mOtrasEnfermedades":false,
"latitud":0.0,
"longitud":0.0
}
}
我处理通知的代码是:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String LOGTAG = "android-fcm";
// private static final int CANCELNOTIFICATIONID = 1;
private Uri ordenUri;
private CamionLocationListener mlocListener;
private LocationManager mlocManager;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
UtilsVictor.appendLog("Ha llegado la notificaciíon");
UtilsVictor.appendLog("Ha llegado la notificaciíon");
LogVictor.d(LOGTAG, "INICIO NOTIFICACIONES");
if (remoteMessage.getData() != null) {
String titulo = remoteMessage.getNotification().getTitle();
LogVictor.d(LOGTAG, "NOTIFICACION RECIBIDA");
LogVictor.d(LOGTAG, "Título: " + titulo);
UtilsVictor.appendLog("Título: " + titulo);
LogVictor.d(LOGTAG, "Texto: " + texto);
UtilsVictor.appendLog("texto: " + texto);
generateNotificationInsertarOrden(getApplicationContext(), texto,remoteMessage.getData());
showNotification22_10_2018(titulo, "NOTIFICACION" , "IDo", this);
}
}
Orden mOrden = new Orden();
private void generateNotificationInsertarOrden(Context context, String message, Map<String, String> data) {
try{
if(null!=message){
Gson gson = new Gson();
String json = gson.toJson(message);
JSONObject jO = new JSONObject(data);
mOrden = gson.fromJson(jO.toString(),Orden.class);
ordenUri = insertarOrdenRecibidaEnBD(mOrden);
Log.v("INSERTADA DE NOTCION:", mOrden.getmId() + ".");
TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoString tareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoString = new TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoString(mOrden.getmID(), Estados.RECIBIDA);
tareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoString.execute("");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, OrdenesOverviewActivity.class);
notificationIntent.putExtra(MyOrdenContentProvider.CONTENT_ITEM_TYPE, ordenUri);
notificationIntent.putExtra("ordenJSON", message);
notificationIntent.putExtra("ESTADO",Estados.NUEVO);
notificationIntent.putExtra("uriPasada",ordenUri);
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context,PendingIntent.FLAG_CANCEL_CURRENT,
notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
// Uri alarmSound = Uri.parse("android.resource://" + getPackageName() + "/raw/sonidoorden.mp3");
Uri alarmSound = Uri.parse("android.resource://es.grainsa.appmovil/" + R.raw.sonidoorden);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setVibrate(new long[] {1000, 1000, 1000})
.setContentText("hay un NUEVO Aviso" + mOrden.getmID()).setSound(alarmSound).build();
notificationManager.notify(0 , notification);
}
} catch (Exception e){
}
}
}
如何在后台管理 JSON???
非常感谢您的回答!
最佳答案
消息有两种“类型”。
具有通知负载/对象的通知。
没有通知对象的数据消息。
当应用程序在后台时,如果消息有通知,系统会处理它......而不是您的 onMessageRecieved() 监听器。
要在前台和后台获取数据,请仅发送“数据消息”,并在应用的 onMessageRecieved() 中创建通知(如果需要)。
即而是传递数据有效负载中的“通知:标题”,并在 onMessageRecieved() 中解析它以创建通知。
当应用程序在后台时,iOS 用户始终需要通知负载来触发应用程序“消息已收到”监听器。在本例中,对于我的工作,我收集 Firebase token 和平台(iOS/Android/Web),并在发送推送时相应地对待每个 token 。
关于java - 在 FCM 通知后台发送 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57420566/