php - 将 iOS 设备 token 存储到 MySQL 数据库

标签 php mysql ios apple-push-notifications

我想准备使用 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 数据库中存储设备 token ,您必须创建一个单独的 API。

例如。调用http://example.com/saveDeviceToken带有设备 token 。

在通知注册(didRegisterForRemoteNotificationsWithDeviceToken)时,您必须使用参数作为您的设备 token 来调用此API。

在此 API 中,您可以获取设备 token 并将其保存到数据库中。 然后在上面的代码中,您只需从数据库中获取设备 token 即可。

关于php - 将 iOS 设备 token 存储到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24298287/

相关文章:

php - Jenkins 无法识别 Composer 命令

ios - 当我在删除文件时取消存档时是否可能崩溃?

mysql - 查询将键值表转换为人类可读的表?

php - 在 MySQLi 和 PHP 中使用数组

ios - 更新到 Parse 1.8.4 pod 后出现 Apple Mach-O Linker 错误

ios - 在 iOS 应用程序中启动

php - 文件不会使用库 Volley ( android/php ) 上传到服务器

php - Ajax /JS : Grabbing multiple input fields without refresh or button click and php echoing the value

mysql - 帮助 PHP 代码,从数据库中选择

php - 如何在 CakePHP 的输入字段中显示数据库中的值