当应用程序在后台时如何从Firebase Push Notif获取消息正文的解码json

时间:2019-07-22 18:29:38

标签: android json firebase

我在Android应用程序中使用firebase push notif,通知从我的服务器发送到firebase api。邮件正文是这样的json字符串

{"id":"36","activity":"DetailActivity","message":"Jatuh tempo pada 23 July 2019","username":"boypanjaitan16","jatuh_tempo":"23 July 2019","jumlah_tagihan":"Rp 78.000,00","perubahan_jumlah_tagihan":"Rp 5.600,00","nilai_denda":"Rp 0,00","status":"BELUM BAYAR"}

因此,当我在firebaseMessagingService中收到此消息时,我首先对其进行了解码,以使“消息”键显示为通知消息正文,而另一个键则用于其他用途。

Intent intent   = new Intent(this, MainActivity.class);


        JSONObject object;
        String body     = null;
        String activity = null;


        try {
            object      = new JSONObject(remoteMessage.getNotification().getBody());
            body        = object.getString("message");
            activity    = object.getString("activity");

            if (activity.equals("DetailActivity")) {
                ResponseDetail detail   = new ResponseDetail();
                Class<?> c = Class.forName("me.panjaitan.boy.penagihan.activity."+activity);
                intent  = new Intent(this, c);

                detail.setId(object.getString("id"));
                detail.setUsername(object.getString("username"));
                detail.setJatuhTempo(object.getString("jatuh_tempo"));
                detail.setJumlahTagihan(object.getString("jumlah_tagihan"));
                detail.setPerubahanJumlah(object.getString("perubahan_jumlah_tagihan"));
                detail.setNilaiDenda(object.getString("nilai_denda"));
                detail.setStatus(object.getString("status"));

                Bundle bundle = new Bundle();
                bundle.putSerializable("data",detail);
                intent.putExtras(bundle);
            }


        } catch (JSONException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId, intent, 0);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, System.ADMIN_CHANNEL_ID)
                .setSmallIcon(R.drawable.wajib_pajak)  //a resource for your custom small icon
                .setContentTitle(remoteMessage.getNotification().getTitle()) //the "title" value you sent in your notification
                .setContentText(body) //ditto
                .setAutoCancel(true)  //dismisses the notification on click
                .setContentIntent(pendingIntent)
                .setSound(defaultSoundUri);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build());
        startActivity(intent);

打开应用程序时没有问题,但是在后台运行时,通知正文变为完整的json字符串。

任何人都可以帮助您,以便它继续显示解码后的json中的“消息”密钥,而不是完整的json字符串?

对不起,我的英语水平

0 个答案:

没有答案