我无法在通知栏/面板中看到推送通知我正在使用https://github.com/phonegap/phonegap-plugin-push。但是,我收到了从我的php服务器发送的消息(它在警报中显示)。任何人都可以告诉我为什么我在android中的通知栏中看不到通知?
这是我在index.html中的java脚本代码。
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;
}