Search code examples
phpandroidjsoncurlfirebase-notifications

How to get FCM notification response as proper JSON format?


I am working with FCM notification for our android application. Here i am having problem with FCM notification response. Which is given below code and response.

<?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;

Response:

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

But above response is not proper json format. Kindly please help me that how to send the FCM notification response as a proper JSON format.

Android code is given below:

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

i am getting response using remoteMessage.getData().

and Firebase ID class is

public class FireIDService extends FirebaseInstanceIdService {

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

Solution

  • Firebase send the Push notification message as a Map object. To get the map as JSON formate you can use the JSONObject class.

    So, The parsing will be like this->

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

    You can also make a POJO class and parse using GSON library.