代码之家  ›  专栏  ›  技术社区  ›  karthik

如何将FCM通知响应作为适当的JSON格式?

  •  1
  • karthik  · 技术社区  · 8 年前

    我正在为我们的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格式。请帮助我如何以正确的JSON格式发送FCM通知响应。

    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 回复  |  直到 8 年前
        1
  •  1
  •   Zahidul Islam    8 年前

    Firebase将推送通知消息作为映射对象发送。要以JSON格式获取映射,可以使用 JSONObject

    因此,解析将如下->

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

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

    推荐文章