Я не могу увидеть push-уведомления в панели уведомлений / панели, которую я использую https://github.com/phonegap/phonegap-plugin-push .Тем не менее, я получаю сообщения, отправленные с моего php-сервера (это отображается в предупреждении). Может ли кто-нибудь сказать мне, почему я не вижу уведомления в панели уведомлений в Android?
Вот мой код Java-скрипта в index.html.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
var push = PushNotification.init({
"android": {
"senderID": "111111111111"},
"ios": {},
"windows": {}
});
push.on('registration', function(data) {
//alert("registration event");
//document.getElementById("regId").innerHTML = data.registrationId;
//alert(data.registrationId);
//console.log(JSON.stringify(data));
var url = 'http://mywebsite.com/reest/rest2.php?id='+data.registrationId;
// alert(url);
$.post( url, function( data ) {
//alert( "Data Loaded: " + data );
});
});
push.on('notification', function(data) {
alert("notification event");
alert(JSON.stringify(data));
var cards = document.getElementById("cards");
var push = '<div class="row">' +
'<div class="col s12 m6">' +
' <div class="card darken-1">' +
' <div class="card-content black-text">' +
' <span class="card-title black-text">' + data.title + '</span>' +
' <p>' + data.message + '</p>' +
' </div>' +
' </div>' +
' </div>' +
'</div>';
cards.innerHTML += push;
});
push.on('error', function(e) {
console.log("push error");
});
}
};
app.initialize();
А здесь ниже мой php-код для отправки сообщения gcm с моего сервера
<?php
//------------------------------
// Payload data you want to send
// to Android device (will be
// accessible via intent extras)
//------------------------------
$data = array( message => 'Hello World! i am app',title => 'Large Icon');
//------------------------------
// 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 = 'apikeyhere';
//------------------------------
// 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;
}
Задача ещё не решена.
Других решений пока нет …