首页 > 解决方案 > 在 FCM 通知后台发送 JSON

问题描述

我知道关于这个话题有几个问题,但我很绝望......

我正在尝试向 Android 手机发送一个可序列化的对象。

当应用程序打开时,Nofitication 工作完美,但在后台显示通知但未正确处理它,只显示标题。

我发送的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 ???

非常感谢您的回答!

标签: javaandroidjsonfirebase

解决方案


有两种“类型”的消息。具有通知有效负载/对象的通知
没有通知对象的
数据消息。

当应用程序在后台时,如果消息有通知,系统会处理它……而不是你的 onMessageRecieved() 监听器。

要在前台和后台获取数据,请仅发送“数据消息”并在应用程序的 onMessageRecieved() 中创建通知(如果需要)。

创建通知 | 安卓

即传递您在数据有效负载中的“通知:标题”,并在 onMessageRecieved() 中对其进行解析以创建通知。

当应用程序在后台时,iOS 用户总是需要通知负载来触发应用程序“收到消息”侦听器。在这种情况下,对于我的工作,我收集了 firebase 令牌和平台(iOS/Android/Web),并在发送推送时相应地对待每一个。


推荐阅读