我目前遇到的问题是我在我的应用上使用的推送通知插件,我真的不会忘记我做错了什么,所以我跳的有人可以指点我正确的方向。这就是我在做的事情:
这是一款具有背景地理位置的应用,我正在使用this插件。 每当用户处于某个半径时,必须进行推送。推送在前景和背景中正确运行(应用程序已打开,但用户按下主按钮)...但如果应用程序已关闭,推送会发出声音,但通知不会显示这不是我想要的,关于背景地理定位一切正常,因为我可以看到屏幕顶部的图标,当应用程序下载时,即使用户不在应用程序上,它也会要求地理定位。我真的认为我没有正确配置推送通知,这是我的代码:
Xml文件:
<plugin name="phonegap-plugin-push" spec="1.8.2" source="npm">
<param name="SENDER_ID" value="myNumber" />
</plugin>
Js档案(我也从here启动了所有内容)
onDeviceReady: function()
{
my code...
app.setupPush();
},
setupPush: function()
{
var push = PushNotification.init({
"android":
{
"senderID": "myNumber"
},
"ios":
{
"sound": true,
"vibration": true,
"badge": true,
"clearBadge": true
},
"windows": {}
});
push.on('registration', function(data)
{
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId)
{
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}
deviceToken = data.registrationId;
});
push.on('error', function(e)
{
console.log("push error = " + e.message);
});
push.on('notification', function(data)
{
push.setApplicationIconBadgeNumber(function()
{
console.log('success');
}, function()
{
console.log('error');
}, data.count + 1);
navigator.notification.confirm(
data.message, // message
onConfirm,
'My Title', // title
['Ok', 'X'] // buttonLabels
);
});
}
Php文件:
计算半径的一些代码,然后:
if ($ambiente == "Android" && $haParceiros >0 )
{
define( 'API_ACCESS_KEY', 'myKey' );
$registrationIds = array( $_GET['token'] );
// prep the bundle
$msg = array
(
'message' => $pushMessage,
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $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;
}
else if ($ambiente == "iOS" && $haParceiros >0 )
{
$device =$deviceToken;
$passphrase = 'myPassphrase';
// Put your alert message here:
$title = $pushMessage;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => array(
'body' => $title,
'action-loc-key' => 'Eco App',
),
'content-available' => 1,
'image' => 'icon-small.png',
'badge' => $haParceiros,
'sound' => 'default',
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
}
就是这样,前景和后台的一切都很好,但是当应用程序关闭时,我已经阅读了一些悬停在网络上的东西,我不知道它是否会影响,例如:
1 - 将deviceToken保存在数据库中(我没有这样做,应用程序在打开时会注册)。
2 - 推送的优先级,我认为我发送ios有效负载并提供内容。
我很抱歉这个问题很长,谢谢你的时间......并且......帮助!!。
此致