gpt4 book ai didi

ios - 使用 IOs-PushPlugin Cordova 2.5 进行首次推送

转载 作者:行者123 更新时间:2023-11-29 03:57:21 25 4
gpt4 key购买 nike

我尝试通过 Holly Schinsky 的教程进行 ddo http://devgirl.org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/

插件已安装,我正在尝试进行第一次推送

在我的index.html中,我从我的src index.js调用函数app.initialize()

这就是我的index.js 中的内容:

var app = {

initialize: function() {
this.bindEvents();
},

bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
app.receivedEvent('deviceready');
},
tokenHandler:function(msg) {
console.log("Token Handler " + msg);
},
errorHandler:function(error) {
console.log("Error Handler " + error);
alert(error);
},

successHandler: function(result) {
alert('Success! Result = '+result)
},

receivedEvent: function(id) {
var pushNotification = window.plugins.pushNotification;
// TODO: Enter your own GCM Sender ID in the register call for Android
if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(this.successHandler, this.errorHandler, {"senderID":"554205989074","ecb":"app.onNotificationGCM"});
}
else {
pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);
},
// iOS
onNotificationAPN: function(event) {
var pushNotification = window.plugins.pushNotification;
console.log("Received a notification! " + event.alert);
console.log("event sound " + event.sound);
console.log("event badge " + event.badge);
console.log("event " + event);
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.badge) {
console.log("Set badge on " + pushNotification);
pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
},
// Android
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
alert('registration id = '+e.regid);
}
break;

case 'message':
// this is the actual push notification. its format depends on the data model
// of the intermediary push server which must also be reflected in GCMIntentService.java
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;

case 'error':
alert('GCM error = '+e.msg);
break;

default:
alert('An unknown GCM event has occurred');
break;
}
}

};

抱歉,代码太多,但我不知道从哪里开始。我对此很陌生,但我会尝试描述发生的事情:

  • 我调用函数app.initialize
  • 我在 XCode 中的输出给出了以下内容:[LOG] Received Event: deviceready
  • 因为我的事件是“deviceready”,所以 app.recievedEvent 应该启动,对吧?
  • 因为我的设备是 IOs-Device,pushNotification.register 应该启动,对吧?

--> 但没有任何反应(除了我在 Xcode 中获取输出:[LOG] Received Event: deviceready)

我的设备上没有收到推送通知


这是我的 php 文件中的代码:

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'cert.pem');


$socketClient = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);


$payload['aps'] = array('alert' => 'test message', 'sound' => 'default', 'badge' => '2');
$payload['id'] = 666;

$payload = json_encode($payload);


$deviceToken = str_replace(' ', '', $data['deviceToken']);


$message = pack('CnH*', 0, 32, $deviceToken);


$message = $message . pack('n', strlen($payload));

$message = $message . $payload;


fwrite($socketClient, $message);


fclose($socketClient);

最佳答案

要接收推送通知,您需要发送通知。您是否已设置服务器来执行此操作?在 DevGirl 的示例中,她使用 node.js 服务器发送推送通知。如果您关注她的帖子,您的手机应该正确设置才能接收通知。

如果您需要推送服务,我有一个 Csharp 代码可以做到这一点

关于ios - 使用 IOs-PushPlugin Cordova 2.5 进行首次推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16335289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com