gpt4 book ai didi

iphone - 推送通知徽章增加和php脚本

转载 作者:行者123 更新时间:2023-12-03 19:43:52 24 4
gpt4 key购买 nike

我使用服务器上的 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,

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

最佳答案

您必须从服务器端执行此操作。就我而言,我是通过 php 和 mysql 完成的。这是我的数据库 enter image description here

我添加了一个字段徽章计数,并且每次使用此代码将推送发送到设备时都会增加徽章计数

    $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

因此,当看到通知时,服务器中的通知又为零。

关于iphone - 推送通知徽章增加和php脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13179636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com