推送通知徽章增加和PHP脚本

时间:2012-11-01 14:52:11

标签: iphone badge

我使用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,

每一次。我究竟做错了什么?你能帮忙吗?谢谢

4 个答案:

答案 0 :(得分:3)

您必须从服务器端执行此操作。在我的情况下,我通过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

因此,当看到通知时,它在服务器中再次为零。

答案 1 :(得分:1)

您无法在有效负载中传递badge的相对值。 +1将简单地变为1.如果要增加或减少,则需要跟踪服务器上的当前徽章编号,并在有效负载中传递新的绝对值。

答案 2 :(得分:0)

我对此并不是100%肯定,但+1只会说1,我认为你需要'+1'。

$body['aps'] = array(
'badge' => '+1',
'alert' => $message,
'sound' => 'default'
);
编辑:实际上,现在考虑到它可能只适用于UrbanAirship ......

答案 3 :(得分:-1)

首先在appDelegate.m

中使用以下方法注册通知
  - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devicetoken { 

Check the reponse here

}