我在開發者論壇上也提過這個問題,但是因爲我的時間不多了,我希望在stackoverflow上張貼可能會更快。Titanium Mobile Push Notifications callback not fired
目前我能夠接收推送通知,並且想要發送一些額外的數據,經過四處搜索後,我認爲這應該是可能的。這個想法是,當推送通知被觸發時,從用戶收件箱(在應用程序中)設置消息,以便用戶得到一個推送通知,指出「新消息」,並且與它一起發送的消息是使用不同的密鑰發送當推送通知callback
函數被調用時,應該保存在Applications.Properties
中。
但是,callback
函數從不觸發。通知被髮送,徽章被設置爲我的php腳本所做的,因此是消息。爲了測試回調是否已觸發,我將徽章和消息更改爲硬編碼值(請參閱下文),我主要從ios muncher獲得了此代碼,但我認爲callback
中的警報將顯示給用戶。
另一件我注意到的事情是,當用戶不使用應用程序時,我只收到推送通知,所以當它在後臺運行時。當用戶使用應用程序推送通知沒有顯示,我認爲這可能是因爲回調沒有觸發。
在此先感謝您的各種幫助。
下面的一些代碼:
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT,
Titanium.Network.NOTIFICATION_TYPE_SOUND
],
success:function(e){
var deviceToken = e.deviceToken;
Ti.API.info("Push notification device token is: "+deviceToken);
//alert('device token is' +e.deviceToken);
var request = Titanium.Network.createHTTPClient();
request.open("POST","http://*********/sendToken.php");
var params = {
"token": e.deviceToken,
"username": authProperties[0].username,
"userId": authProperties[0].userId
};
request.send(params);
Ti.API.info("Push notification types: "+Titanium.Network.remoteNotificationTypes);
Ti.API.info("Push notification enabled:"+Titanium.Network.remoteNotificationsEnabled);
},
error:function(e){
alert("Error during registration: "+e.error);
Ti.API.info("Error during registration: "+e.error);
},
callback:function(e)
{
// called when a push notification is received.
//Titanium.Media.vibrate();
var data = JSON.parse(e.data);
request.open("POST","http://*********/callback.php");
var params = {
"token": e.deviceToken,
"username": authProperties[0].username,
"userId": authProperties[0].userId
};
request.send(params);
// Message data for the inbox
var inboxData = data.inbox;
Titanium.App.properties.setString("badgeCount",data.badge);
var badge = data.badge;
if(badge > 0){
Titanium.UI.iPhone.appBadge = 202;//badge;
}
var message = data.message;
if(message != ''){
var my_alert=Ti.UI.createAlertDialog({title:'',message:JSON.stringify(inboxData) });
my_alert.show();
}
}
});
下面的腳本處理推送通知的PHP的一部分:
$serverId = "81273";
$name = "APNS";
$apnsPort = 2195;//5223;
$passPhrase = "";
$fwrite = "";
$sslUrl = "ssl://gateway.push.apple.com:" . $apnsPort;
$apnsCert = "./apns-distr.pem";//give the apns.pem file path on your server
$badge = 22;
$message = "[". date("d-m-Y h:i:s") . '] Er is een nieuw bericht voor u.';
$inboxArray = array();
$inboxArray["id"]= 1;
$inboxArray["message"] = "Dit bericht dient als test";
$inboxArray["date"] = date("d-m-Y h:i:s");
$apnspayload['aps'] = array ('alert' => $message,'badge' => $badge,'sound' => 'default', 'inbox' => $inboxArray);
$payload = json_encode($apnspayload);
$tokens = array();
$tokens[] = "********** ** * * *";
foreach($tokens as $tokenId){
$apnsMessage = chr(1) . pack('N', time()) . pack('N', time() + 86400) . chr(0) . chr(32)
. pack('H*', str_replace(' ', '', $tokenId)) . chr(0) . chr(strlen($payload)) . $payload;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $passPhrase);
$apns = stream_socket_client($sslUrl, $error, $errorString, 6, STREAM_CLIENT_CONNECT, $streamContext);
if($apns){
$fwrite = fwrite($apns, $apnsMessage);
fclose($apns);
@socket_close($apns);
}else{
echo 'request failed';
}
}`
這個問題似乎與您最近的問題不一致,您不知道如何避免在所有上下文中觸發回調。 ..你有解決這個問題嗎? http://stackoverflow.com/questions/14231470/titanium-mobile-apple-push-notifications-what-is-the-most-logical-place-to-ins –
你提到的問題已經解決了,另外一個提出了。 。解析JSON似乎有些問題。 –