2012-11-01 73 views
2

我用我的服務器的PHP腳本發送推送通知到我的設備。腳本代碼是推送通知徽章增加和php腳本

<?php  
// Put your device token here (without spaces): 
$deviceToken = 'a14b6212fa69a2b1c2dde4547a50c711fd40b9787cc029800584890d72a9f5db'; 

// Put your private key's passphrase here: 
$passphrase = 'ujyfljnhjgby123'; 

// Put your alert message here: 
$message = 'Delivery 33 message!'; 

//////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err, 
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp) 
exit("Failed to connect: $err $errstr" . PHP_EOL); 

echo 'Connected to APNS' . PHP_EOL; 

// Create the payload body 
$body['aps'] = array(
'badge' => +1, 
'alert' => $message, 
'sound' => 'default' 
); 

// Encode the payload as JSON 
$payload = json_encode($body); 

// Build the binary notification 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .  $payload; 

// Send it to the server 
$result = fwrite($fp, $msg, strlen($msg)); 

if (!$result) 
echo 'Message not delivered' . PHP_EOL; 
else 
echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 

在我AppDelegate.m有這樣的代碼

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue]; 

} 

的問題是 - 爲什麼在我的工號,當我收到多個通知沒有更新?看起來像它的只是替換

'badge' => +1, 

每次。我究竟做錯了什麼?你能幫忙嗎?謝謝

回答

2

你必須從服務器端做到這一點。在我的情況下,我已經通過PHP和MySQL做到了。這裏是我的數據庫 enter image description here

我添加一個字段badgecount,我增加了徽章計數每次我發推送到設備使用此代碼

$query = "SELECT badgecount FROM pushnotifications WHERE device_token = '{$device_token}'"; 
    $query = $this->db->query($query); 
    $row = $query->row_array(); 
    $updatequery = "update pushnotifications set badgecount=badgecount+1 WHERE device_token ='{$device_token}'"; 
    $updatequery = $this->db->query($updatequery); 
    $device = $device_token; 
    $payload['aps'] = array('alert' => $pushmessage, 'badge' =>$row["badgecount"]+1, 'sound' => 'default'); 
    $payload = json_encode($payload); 

而且我還讓另一個API製作的時間badgcount 0在

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 

因此,當通知被看到它在服務器中再次爲零。

+0

你救了我的一天:) –

-1

使用以下方法通知第一寄存器中appDelegate.m

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devicetoken { 

Check the reponse here 

} 
+0

我有這種方法。我可以收到通知,我的問題是徽章上的數字不會增加,當我發送超過1通知 – NewObjective

1

您不能在有效載荷badge通過相對值。 +1將簡單地變爲1.如果你想增加或減少,你將需要跟蹤服務器上的當前徽章號碼,並在有效載荷中傳遞新的絕對值。

+0

所以你的意思是我需要發送到服務器我當前的徽章號碼? – NewObjective

+0

如果您更改應用程序內的徽章號碼,是的。例如,當用戶在您的應用中打開未讀郵件時,您需要更新徽章號碼。然後告訴服務器新的徽章號碼,或者讓它將郵件標記爲已讀,並讓它找出正確的徽章號碼。這取決於它是什麼類型的應用程序以及您的服務器API的結構。 – Rengers

+0

@Rengers你可以詳細說明一下,我現在面臨這個問題。我只是不知道如何跟蹤我的服務器上的徽章數量 – 2014-03-28 21:41:39

0

我對此並不是100%肯定的,但+1會說1,我認爲你需要'+1'。

$body['aps'] = array(
'badge' => '+1', 
'alert' => $message, 
'sound' => 'default' 
); 

編輯:其實,這可能只是爲UrbanAirship現在,我想想...