我有一个Android / ios应用程序,我在php中使用浏览器在数据库中插入文章,然后该应用程序使用ajax从数据库中推送数据。
我需要的是,当我在php(服务器端)上发布文章并插入时,它会向手机发送通知。
我在我的应用js上有这个
const push = PushNotification.init({
android: {
vibrate: "true",
alert: "true",
badge: "true",
sound: "true"
},
browser: {
},
ios: {
vibrate: "true",
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});
push.on('registration', function(data) {
//alert(data.registrationId);
var registrationId=data.registrationId;
var dataString ="id_usuario="+registrationId;
if($.trim(registrationId).length>0){
$.ajax({
type: "POST",
url: "https://pizzarte.com/app/registarid.php",
data: dataString,
crossDomain: true,
cache: false,
})
}
});
push.on('notification', function(data) {
//alert(data.title+" Message: grgregregerg" +data.message);
});
push.on('error', function(e) {
alert(e);
});
因此,当用户启动应用程序时,它将保存该设备的registrationID,并将其存储在数据库中。这很好。现在在服务器端,我拉出所有注册的ID,并尝试发送通知:
<?php
// API access key from Google API's Console - server key here https://console.developers.google.com/apis/credentials?authuser=2&project=project_name
define( 'API_ACCESS_KEY', 'this is my api access key, im using the server key');
$registrationIds = array("device_ids");
var_dump ($registrationIds);
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
var_dump ($headers);
$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;
但是我总是收到错误“未经授权 错误401“
在我的config.xml上我也有这个:
<plugin name="phonegap-plugin-push" spec="2.0.0" />
<variable name="SENDER_ID" value="xxxxxxxxxxxxx" />
</plugin>
<platform name="android">
<preference name="android-minSdkVersion" value="14" />
<allow-intent href="market:*" />
<resource-file src="google-services.json" target="google-services.json" />
</platform>
我在这里做什么错了?
答案 0 :(得分:0)
在config.xml中更改它
<plugin name="phonegap-plugin-push" spec="~2.1.2" >
<variable name="FCM_VERSION" value="11.0.1" />
</plugin>
然后更改
<platform name="android">
<preference name="android-minSdkVersion" value="14" />
<allow-intent href="market:*" />
<resource-file src="google-services.json" target="google-services.json" />
</platform>
到
<platform name="android">
<preference name="android-minSdkVersion" value="16" />
<allow-intent href="market:*" />
<resource-file src="google-services.json" target="app/google-services.json" />
</platform>
value =“ 14”到value =“ 16”
target =“ google-services.json”必须为target =“ app / google-services.json”
希望它能对您有所帮助。