java - 在 FCM 通知后台发送 JSON

标签 java android json firebase

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

我正在尝试将可序列化对象发送到 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() 中创建通知(如果需要)。

Create Notification | Android

即而是传递数据有效负载中的“通知:标题”,并在 onMessageRecieved() 中解析它以创建通知。

当应用程序在后台时,iOS 用户始终需要通知负载来触发应用程序“消息已收到”监听器。在本例中,对于我的工作,我收集 Firebase token 和平台(iOS/Android/Web),并在发送推送时相应地对待每个 token 。

关于java - 在 FCM 通知后台发送 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57420566/

相关文章:

java - Spring Bean 可以在不同地方使用还是在一个地方使用

java - 无法在源集主上找到属性 'allJava'

android - 如何降低 Android 屏幕分辨率以支持低内存

javascript - 如何基于 JSON 创建 HTML 表格

java - (架构)为 angular2 应用程序抓取数据。直接检查MongoDB还是我的Java REST?

java - 共享首选项不起作用

java - Spring 存储库中的 PostgreSQL 参数化查询

android - 应用程序启动时如何在谷歌地图中显示特定城市的 map

javascript - 对象的顺序已更改

javascript - 当按值中的第一个字符对 JSON 对象数组进行分组时,如何将以数字开头的所有字符分组到同一组中?