繁体   English   中英

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

[英]Send variables to iOS from PHP push notification?

我正在使用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;
}

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

我什至尝试了这个:

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

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

提前致谢!

NSDictionary *pushInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"aps"]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM