2013-05-02 70 views
0

先推我试图 http://devgirl.org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/与IOS-PushPlugin科尔多瓦2.5

插件安装到距离Holly Schinsky提供DDO本教程中,我试图让我的第一推

在我的index.html我把从我的SRC功能app.initialize()index.js

所以这是,在我的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中输出给了我这样的:[日志]接收到的事件:deviceready
  • 因为我的事件是'deviceready',那么app.recievedEvent应该开始,对吧?
  • 因为我的设备是一个IOs设备pushNotification.register应该启动,对不对?

- 但>什么也没有发生(我旁边#m如果您在Xcode输出:[日志]接收到的事件: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); 

回答

1

要接收推送通知,您需要发送一个通知。你有没有设置一个服务器来做到这一点。以DevGirl为例,她使用node.js服务器发送推送通知。 如果你按照她的帖子,你的手机应设置正确,以接收通知。

如果你需要的推送服务,我有一个Csharp的代码来做到这一点

+0

哦好的,谢谢。我在这个教程中认为一切都设置正确,我应该收到像截图一样的通知。这很有道理,但事实并非如此。我曾经有一个Xcode应用程序(没有phonegap),我从我的服务器发送来自外部php文件的通知 - 请参阅其他答案中的代码。是否有可能从这个文件发送通知到苹果通知中心到我的设备?我还没有做过与node.js或Csharp的事情呢... – 2013-05-03 15:32:38

+0

请参阅下面的答案 – 2013-05-03 21:13:38