从 PHP 推送通知发送变量到 iOS?

发布于 2024-11-27 06:28:07 字数 1942 浏览 2 评论 0原文

我正在使用 PHP 脚本将推送通知发送到我的设备。这是我使用的,

$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(
'alert' => $message,
'sound' => 'default',
'id' => '10'
);

// 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;

echo "to $deviceToken.";

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

我成功了。我点击视图按钮,然后尝试像这样读取“id”变量:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Check for push notification info  
NSDictionary *pushInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];  
if (pushInfo)  
{  
    // TODO: Pull the poke's info from our server and update the UI to display it  
    NSLog(@"Here's the id: %@", [pushInfo valueForKey:@"id"]);  
}

return YES;
}

但是,在多任务处理时我没有得到 NSLog。关闭应用程序时我还没有尝试过,因为关闭后我无法运行控制台。

我什至尝试过这个:

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);
}

如果我在运行时发送通知,我什至没有得到 NSLog。这是怎么回事?

提前致谢!

I am using a PHP script to send push notifications to my device. Here is what I use

$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(
'alert' => $message,
'sound' => 'default',
'id' => '10'
);

// 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;

echo "to $deviceToken.";

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

I get it successfully. I tap the view button, and I am trying to read the 'id' variable like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Check for push notification info  
NSDictionary *pushInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];  
if (pushInfo)  
{  
    // TODO: Pull the poke's info from our server and update the UI to display it  
    NSLog(@"Here's the id: %@", [pushInfo valueForKey:@"id"]);  
}

return YES;
}

However, I don't get a NSLog when in mutitasking. I have yet to try it when I close the app because I cannot run console after you close out.

I even tried this:

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);
}

If I send the notification when running, I don't even get a NSLog. What's going on?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2024-12-04 06:28:07
NSDictionary *pushInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"aps"]];
NSDictionary *pushInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"aps"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文