【发布时间】:2015-01-29 21:33:04
【问题描述】:
我正在尝试向 APNS 发送推送通知,调试我的服务器进行身份验证并发送通知,但 iphone 上没有显示任何内容。
我正在使用
<gap:plugin name="com.phonegap.plugins.pushplugin" version="2.4.0" />
我可以使用以下代码捕获 deviceToken 以发送推送:
if (device.platform == "iOS"){
try
{
var pushNotification = window.plugins.pushNotification;
if (pushNotification != undefined){
pushNotification.register(
tokenHandler,
errorHandler, {
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN"
});
}
}
catch(e)
{
alert('erro ao registrar ios: ' + e.message + ', stack: ' + e.stack);
}
}
通知:
function onNotificationAPN(event) {
alert('notificationapn: ' + JSON.stringify(event));
try
{
if (event.alert)
{
if (event.foreground == 1){
if (event.alertaID != undefined){
navigator.notification.alert(
event.alertaCorpo,
function(){ },
'Mensagem de Incentivo',
'Ok'
);
}
else if (event.lembrete != undefined){
navigator.notification.alert(
event.alert,
function(){ },
'Lembrete',
'Ok'
);
}
}
else
{
if (event.alertaID != undefined){
mostraPopupAguarde();
var mensagensIncentivo = new Array();
var alerta = new Object();
alerta.ID = event.alertaID;
alerta.UrlImagem = event.alertaUrlImagem;
alerta.Nome = event.alertaNome;
alerta.Corpo = event.alertaCorpo;
mensagensIncentivo.push(alerta);
window.localStorage.setItem('mensagensIncentivo', JSON.stringify(mensagensIncentivo));
carregaMensagemIncentivo(alerta.ID);
ocultaPopupAguarde();
}
else if (event.lembrete != undefined){
navigator.notification.alert(
event.alert,
function(){ },
'Lembrete',
'Ok'
);
}
}
}
if ( event.sound )
{
var snd = new Media(event.sound);
snd.play();
}
if ( event.badge )
{
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
catch(e)
{
alert('erro ao registrar ios: ' + e.message + ', stack: ' + e.stack);
}
}
以及带有令牌的代码:
function tokenHandler(result) {
//alert('Token: ' + result);
//I send token to the server.
}
我从服务器到 APNS 的消息:
{"aps":{"alert":"Você tem um(a) Hidracor agendado(a)!","badge":1,"sound":"new.caf"},"lembrete": true}
我下载了 aps_development.cer 并使用 openssl 执行了以下命令:
openssl x509 -in "aps_development.cer" -inform DER -out "path_to_an_output_Cert.pem" -outform PEM
openssl pkcs12 -export -inkey ios.key -in aps_development.pem -out aps_development.p12
openssl pkcs12 -nocerts -in "aps_development.p12" -out "path_to_an_output_Key.pem" -passin pass:mykey -passout pass:mykey
openssl pkcs12 -export -inkey "path_to_an_output_Key.pem" -in "path_to_an_output_Cert.pem" -out "pushCert.p12" -passin pass:mykey -passout pass:mykey
我使用这个文件 pushCert.p12 通过服务器使用 APNS 进行身份验证。
我用这个例子来连接APNS:
http://apns-c-sharp-net-vikram-jain.blogspot.com.br/
谁能帮帮我?
谢谢!
【问题讨论】:
标签: c# ios iphone apple-push-notifications phonegap-plugins