我使用“ react-native-push-notification”库将推送通知集成在react native中。 当应用程序关闭时,推送通知消息将正确显示标题和消息,但是当应用程序打开时,通知消息将显示为JSON字符串,例如{“ title”:“ Notification Title”,“ body”:“ This is test message”}
反应本机代码
var PushNotification = require("react-native-push-notification");
PushNotification.configure({
onRegister: function(token) {
console.log("TOKEN:", token);
},
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
senderID: "Sender id",
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true
});
PHP脚本
define('API_ACCESS_KEY',API_ACCESS_KEY);
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token='token';
$notification = [
'title' =>'Test App',
'body' => 'This is test message',
];
$extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
$fcmNotification = [
//'registration_ids' => $tokenList, //multple token array
'to' => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);
echo $result;