我想为APNS的生产做好准备,我假设最好的(唯一,最简单的?)方法是存储在数据库中注册推送的设备id(假设SQL),然后让PHP脚本(以某种方式?)将其发送给所有人(for循环?)而不是像我现在一样只有一个(deviceToken
-我的)。
这是我目前在AppDelegate.m中的内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@“Token: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
这里记录的是用户deviceToken,然后手动复制粘贴到PHP脚本中就像这样:
<?php
// Put your device token here (without spaces):
$deviceToken = '[device token here]';
// Put your private key's passphrase here:
$passphrase = '[passphrase here]';
// Put your alert message here:
$message = 'Hello, there!';
////////////////////////////////////////////////////////////////////////////////
$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'
);
// 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);
所以我需要帮助的是连接到我的服务器的数据库(MySQL)和存储deviceToken
。然后我需要循环PHP将通知发送到所有设备。
如果我需要澄清什么,请告诉我。
谢谢。
为了在MySQL数据库中存储设备令牌,您必须创建一个单独的API。
例如使用设备令牌调用http://example.com/saveDeviceToken。
在通知注册(didRegisterForRemoteNotificationsWithDeviceToken)的时候,你必须调用这个API参数作为你的设备令牌。
在这个API中,您可以获取设备令牌并将其保存到数据库中。然后在上面的代码中,您只需从DB中获取设备令牌。