2013-05-02 68 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