我正在使用FIREBASE CLOUD MESSAGING服务与我的离子产品和 phonegap-plugin-push cordova插件来获取PHP BACK END的推送通知。
当我试图获得推送通知时,php结束获得成功,如下所示。
示例推送数据有效负载
{" multicast_id":8853634389214913500,"成功":1,"失效":0," canonical_ids":0,&# 34;结果":[{" MESSAGE_ID":" 0:1495614850271706%39688dd8f9fd7ecd"}]}
技术规范:
cordova推送通知插件版本:1.9.4
平台和版本:Ionic V1
Ionic CLI版本:2.1.13
Cordova版本:cordova --6.4.0
适用于cordova的Android平台:6.0.0
Android)我测试的设备供应商:三星,HUWAWEI,小米 等
示例代码说明了以下问题
IONIC PART:
//推送通知 if(window.cordova){ if(!localStorage.getItem(' device_token')){ var apkId = 0; var iosId = 0; var options = { android:{ senderID:MY FCM SENDER ID, icon:" alert", }, ios:{ 警告:" true", 徽章:" true", 声音:"真" }, 窗口:{} };
//localStorage.getItem('gcmRegId')
// initialize
$cordovaPushV5.initialize(options).then(function () {
// start listening for new notifications
$cordovaPushV5.onNotification();
// start listening for errors
$cordovaPushV5.onError();
// register to get registrationId
$cordovaPushV5.register().then(function (data) {
//alert("GCM"+data);
// if Android device.
if (ionic.Platform.isAndroid()) {
apkId = data;
}
// if ios device.
if (ionic.Platform.isIOS()) {
iosId = data;
}
// Updating member details with apkId or iosId
var pushParams = {
'app_token': Config.appToken,
'device_uiu_token': device.uuid,
'apk_token': apkId,
'ios_token': iosId
}
$http.post(Config.apiUrl + "member/save_token", pushParams)
.success(function (data) {
if (data.status == 200) {
localStorage.setItem("device_token", device.uuid);
}
/* else{
alert("Sorry!Error occurs!");
} */
});
})
// Updating end.
});
// triggered every time notification received
$rootScope.$on('$cordovaPushV5:notificationReceived', function (event, data) {
alert("recieved" + JSON.stringify(data));
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});
// triggered every time error occurs
$rootScope.$on('$cordovaPushV5:errorOcurred', function (event, e) {
alert('push ERROR' + e.message);
// e.message
});
//推送通知结束
PHP PART:
$push_title = $this->input->post('push_title');
$push_msg = $this->input->post('push_msg');
$members = $this->members_model->get_members();
$apk_tokens = array();
$ios_tokens = array();
foreach ($members as $member) {
if ($member['apk_token'] != 0 || $member['apk_token'] != "") {
array_push($apk_tokens, $member['apk_token']);
}
if ($member['ios_token'] != 0 || $member['ios_token'] != "") {
array_push($ios_tokens, $member['ios_token']);
}
}
//Sending the push notification using GCM.
$msg = array(
'message' => $push_msg,
'title' => $push_title,
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon',
);
$fields = array
(
'registration_ids' => $apk_tokens,
'data' => $msg,
'priority' => 'high'
);
$headers = array
(
'Authorization: MY FCM SERVER KEY',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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;
提前致谢!
答案 0 :(得分:0)
如果您想在锁定屏幕上显示通知,请使用notification
中的$fields
。该对象需要title
和body
个元素。
https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol
$fields = array
(
'registration_ids' => $apk_tokens,
'data' => $msg,
'priority' => 'high',
'notification' => array(
'title' => 'This is title',
'body' => 'This is body'
)
);
我没有尝试此代码,但node.js SDK的相同问题已经以这种方式修复。