首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在FCM通知背景下发送JSON

在FCM通知背景下发送JSON
EN

Stack Overflow用户
提问于 2019-08-08 21:04:04
回答 1查看 75关注 0票数 0

我知道这是几个关于这个话题的问题,但我不知道.

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

当应用程序打开时,Nofitication工作得很好,但是在后台,通知会出现,但处理不正确,只显示标题。

我要发送的信是:

代码语言:javascript
复制
{  


"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
   }
}

当我处理通知时,代码是:

代码语言:javascript
复制
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?

非常感谢你的回答!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-08 21:11:09

有两种“类型”的消息。

通知,它有一个Notification /对象。

数据消息,它没有通知Obj。

当应用程序处于后台时,如果消息有通知,系统将处理it...not您的onMessageRecieved()侦听器。

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

创建通知- Android

即传递数据有效负载中的“Notification: title”,然后在onMessageRecieved()中解析它以创建通知。

当应用程序处于后台时,iOS用户总是需要一个通知有效负载来触发应用程序“消息接收”侦听器。在本例中,我收集防火墙令牌和平台(iOS/Android/Web),并在发送推送时对每个令牌进行相应处理。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57420566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档