我正在尝试使用phonegap构建开发移动网络应用,并通过亚马逊SNS实施推送通知。
我目前正在Android上测试,我设法让应用程序处于后台或前台时通知工作,但如果我完全杀死应用程序,则不会收到通知。
一旦我启动该应用程序,它就会收到“等待”#39;通知。
我做了大量的搜索,但没有找到解决方案,因为我在后台和前台收到通知,而且我没有从AWS收到任何错误,我认为设备和服务器端的注册是正确的。
即使应用程序完全关闭,我如何才能在标准状态栏中收到通知?
这里有一些代码:
客户端
jQuery(document).ready(function($) {
document.addEventListener("deviceready", onDeviceReady, false);
});
function onDeviceReady(){
pushNotificationFlow();
function pushNotificationFlow(){
var pushNotification = window.plugins.pushNotification;
window.GCMsenderID = '123456789';
if(isMobile.Android()){
//android
pushNotification.register(successHandler, errorHandler,{"senderID": window.GCMsenderID,"ecb":"onNotificationGCM"});
}
else{
//ios
pushNotification.register(tokenHandler, errorHandler,{
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPNS" });
}
function tokenHandler (result) {
var idUser=getLocalUser();
if ( result.length > 0 ){
$.post('http://myserver.com/my_serverside_registration.php', {
token: result,
shop: window.shop,
type: 'APNS',
id_user: idUser
},function( data ) {
});
}
}
function successHandler (result) {
}
function errorHandler (error) {
}
}
function onNotificationGCM (e) {
$.post('http://pointcard.stayted.com/pushTest.php', {
log: JSON.stringify(e)
});
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
var idUser=getLocalUser();
$.post('http://myserver.com/my_serverside_registration.php', {
token: e.regid,
shop: window.shop,
type: 'GCM',
id_user: idUser
},function( data ) {
});
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;
case 'error':
break;
default:
break;
}
}
function onNotificationAPNS (e) {
if ( e.alert )
{
chocolatApp.addNotification({
title: 'mytitle',
message: e.alert,
media: '<img width="44" height="44" style="border-radius:100%" src="dist/img/appIcon.png">'
});
}
if ( e.sound )
{
var snd = new Media(e.sound);
snd.play();
}
if ( e.badge )
{
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, e.badge);
}}
SERVER
//create amazon SNS client
require_once dirname(__FILE__).'/vendor/autoload.php';
use Aws\Sns\SnsClient;
$message = 'test message '.date('d/m/Y G:i',time());
$title= 'mytitle';
$AWS_apiKey = 'my_apiKey';
$AWS_apiSecret = 'my_apiSecret';
$region = 'region';
$topicArn = 'my_topicArn';
$endpointArn = 'my_endpointArn';
// Instantiate with your AWS credentials
$client = SnsClient::factory(array(
'credentials' => array(
'key' => $AWS_apiKey,
'secret' => $AWS_apiSecret,
),
'region' => $region
));
//Publish
try{
$array = array(
//'TopicArn' => $topicArn,
'TargetArn' => $endpointArn,
// Message is required
'Message' => json_encode(array(
'default' => 'default',
'APNS' => json_encode(array(
'aps' => array(
'alert' => $message,
"sound" => "default",
),
)),
'APNS_SANDBOX' => json_encode(array(
'aps' => array(
'alert' => $message,
"sound" => "default",
),
)),
'GCM' => json_encode(array(
'data' => array(
'message' => $message,
'title' => $title,
)
)),
)),
'MessageStructure' => 'json',
'MessageAttributes' => array(
// Associative array of custom 'String' key names
'String' => array(
// DataType is required
'DataType' => 'String',
'StringValue' => 'String'
//'BinaryValue' => 'String',
),
),
);
$messageID = $client->publish($array);
}catch (\Exception $e) {
$error = $e->getMessage();
}
if(!empty($error)){
$alert.="<div class=\"alert alert-danger\">
<strong>Error</strong>: ".print_r($error,true)."
</div>";
}
elseif(!empty($message) AND empty($alert) ){
$alert.="<div class=\"alert alert-success\">
<strong>Ok.</strong> success.
</div>";
}
}
编辑:我指的是从最近关闭应用程序&#39;不是强制停止设置 - &gt;应用
答案 0 :(得分:2)
尝试从服务器端使用一些现成的推送通知平台,如PubNub: https://www.pubnub.com/blog/2015-06-23-getting-started-with-gcm-android-push-notifications-and-pubnub/ https://www.pubnub.com/blog/2015-06-24-sending-receiving-android-push-notifications-with-gcm-google-cloud-messaging/ 我也在使用它们,它们工作正常。
答案 1 :(得分:1)
尝试使用
<uses-permission android:name="android.permission.WAKE_LOCK" />