InvalidRegistration无效注册我使用了phonegap-plugin-push "success":0,"failure":1,"canonical_ids":0,"results":{"error":"InvalidRegistration"}}
我正在尝试https://github.com/phonegap/phonegap-plugin-push phonegap的最新插件,下面是我的java脚本代码和php代码
并使用离子cordova
PHP代码
enter code here
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyBEzOLapgBPKJ-c_NU8rVsoU6k2e_Q-YqA' );
echo $_GET['id'];
$registrationIds = array( $_GET['id'] );//
// 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,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => array($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;在这里获取和发送注册id的javascript代码
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var push = PushNotification.init({ "android": {"senderID": "280872888677"},
"ios": {}, "windows": {} } );
push.on('registration', function(data) {
var url = 'http://mywebsite.com/reest/pushn.php? id='+data.registrationId;
alert(url);
//post url
$.post( url, function( data ) {
alert( "Data Loaded: " + data );});});
push.on('notification', function(data) {
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});
push.on('error', function(e) {
alert(e.message);
});
}
</script>发布于 2015-09-01 22:52:07
实际上,我试着使用这个库,但我遇到了同样的问题,然后我决定改变这个库,使用PushPlugin,它非常容易使用和直接。下面是它的链接:https://github.com/phonegap-build/PushPlugin
发布于 2015-09-05 16:34:03
我通过将php代码编辑成下面的代码解决了这个问题。
//------------------------------
// Payload data you want to send
// to Android device (will be
// accessible via intent extras)
//------------------------------
$data = array( 'message' => 'Hello World!' );
//------------------------------
// The recipient registration IDs
// that will receive the push
// (Should be stored in your DB)
//
// Read about it here:
// http://developer.android.com/google/gcm/
//------------------------------
$ids = array( $_GET['id'] );//array( 'abc', 'def' );
//------------------------------
// Call our custom GCM function
//------------------------------
sendGoogleCloudMessage( $data, $ids );
//------------------------------
// Define custom GCM function
//------------------------------
function sendGoogleCloudMessage( $data, $ids )
{
//------------------------------
// Replace with real GCM API
// key from Google APIs Console
//
// https://code.google.com/apis/console/
//------------------------------
$apiKey = 'apikey';
//------------------------------
// Define URL to GCM endpoint
//------------------------------
$url = 'https://android.googleapis.com/gcm/send';
//------------------------------
// Set GCM post variables
// (Device IDs and push payload)
//------------------------------
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
//------------------------------
// Set CURL request headers
// (Authentication and type)
//------------------------------
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
//------------------------------
// Initialize curl handle
//------------------------------
$ch = curl_init();
//------------------------------
// Set URL to GCM endpoint
//------------------------------
curl_setopt( $ch, CURLOPT_URL, $url );
//------------------------------
// Set request method to POST
//------------------------------
curl_setopt( $ch, CURLOPT_POST, true );
//------------------------------
// Set our custom headers
//------------------------------
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
//------------------------------
// Get the response back as
// string instead of printing it
//------------------------------
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
//------------------------------
// Set post data as JSON
//------------------------------
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
//------------------------------
// Actually send the push!
//------------------------------
$result = curl_exec( $ch );
//------------------------------
// Error? Display it!
//------------------------------
if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}
//------------------------------
// Close curl handle
//------------------------------
curl_close( $ch );
//------------------------------
// Debug GCM response
//------------------------------
echo $result;
}https://stackoverflow.com/questions/32333423
复制相似问题