如何以正确的 JSON 格式获取 FCM 通知响应?
How to get FCM notification response as proper JSON format?
我正在为我们的 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 class 是
public class FireIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String tkn = FirebaseInstanceId.getInstance().getToken();
Log.e("tkn","tkn---->"+tkn);
}
}
Firebase 将推送通知消息作为地图对象发送。要将地图作为 JSON 格式,您可以使用 JSONObject
class.
所以,解析会是这样->
JSONObject jsonData = new JSONObject(remoteMessage.getData());
您还可以创建一个 POJO class 并使用 GSON 库进行解析。
我正在为我们的 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 class 是
public class FireIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String tkn = FirebaseInstanceId.getInstance().getToken();
Log.e("tkn","tkn---->"+tkn);
}
}
Firebase 将推送通知消息作为地图对象发送。要将地图作为 JSON 格式,您可以使用 JSONObject
class.
所以,解析会是这样->
JSONObject jsonData = new JSONObject(remoteMessage.getData());
您还可以创建一个 POJO class 并使用 GSON 库进行解析。