如何将FCM通知响应作为正确的JSON格式?

时间:2017-10-31 05:09:33

标签: php android json curl firebase-notifications

我正在处理我们的Android应用程序的FCM通知。这里我遇到了FCM通知响应问题。下面给出了代码和响应。

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxx' );
$registrationIds = array('id'=>'xxxxxxxxxxxxxxxx');
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title' => 'This is a title. title',
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'      => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

响应:

{message = here is a message. message, title = This is a title. title }

但上面的反应不是正确的json格式。请帮助我如何将FCM通知响应作为正确的JSON格式发送。

Android代码如下:

public class FireMsgService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.e("remoteMessage-", "remoteMessage--->" + remoteMessage.getData());
        // Log.e("getnotification-", "getnotification--->" + remoteMessage.getNotification());
        Intent intent = new Intent(this, EventFragment.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                //.setSmallIcon(getNotificationIcon())
                .setContentTitle("Event Application")
                .setContentText("Quiz notification")
                .setAutoCancel(true)
                .setSound(sound)
                .setContentIntent(pendingIntent)
                .setSmallIcon(getNotificationIcon());
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.a_option : R.drawable.a_option;
    }
}

我正在使用remoteMessage.getData()获得响应。

和Firebase ID类

public class FireIDService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String tkn = FirebaseInstanceId.getInstance().getToken();
        Log.e("tkn","tkn---->"+tkn);
    }
}

1 个答案:

答案 0 :(得分:1)

Firebase将推送通知消息作为Map对象发送。要将地图作为JSON格式化,您可以使用JSONObject类。

所以,解析就像这样 - &gt;

JSONObject jsonData = new JSONObject(remoteMessage.getData());

您还可以创建POJO类并使用GSON库进行解析。