php – 如何获取设备令牌

我有一个iOS应用程序,我想集成推送通知.我见过
tutorial on youtube,一切都还可以,但最近我使用的是开发证书(用于测试 – 不用于AppStore),我的服务器上有PHP脚本.在这个文件中存储了deviceToken,它有我的iPhone并且用php变量$deviceToken编写.但是现在,当我想在AppStore中使用它时,如何从已下载我的应用程序并将其加入PHP脚本的每个人那里获取设备令牌?

这是我的PHP文件:

 if($_POST['message']){

        $deviceToken = '(my device token)';

        $message = stripslashes($_POST['message']);

        $payload = '{
                        "aps" : 

                            { "alert" : "'.$message.'",
                              "badge" : 1,
                              "sound" : "bingbong.aiff"
                            } 
                    }';

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');
        $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        if(!$fp){
            print "Failed to connect $err $errstrn";
            return;
        } else {
            print "DONE!";
        }

        $devArray = array();
        $devArray[] = $deviceToken;

        foreach($devArray as $deviceToken){
            $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack        ("n",strlen($payload)) . $payload;
            fwrite($fp, $msg);
        }
        fclose($fp);
    }

<form action="send-notification.php" method="post">
    <input type="text" name="message" maxlength="100">
    <input type="submit" value="SEND">
</form>
</body>

这就是我在xCode(AppDelegate.m)中所拥有的

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
    NSLog(deviceTokenString);
}

最佳答案 好吧,我不懂PHP,所以我不能给你具体的代码,但我可以解释一般原理.

当您的应用程序启动时,您应该通过调用registerForRemoteNotificationTypes:方法注册到Apple推送通知.

当注册成功并且您获得设备令牌时,您应该在应用程序的实现中将其发送到您的服务器:didRegisterForRemoteNotificationsWithDeviceToken:.

您的服务器应将其存储在某些数据库中.

发送通知的PHP脚本应该从该数据库获取设备令牌.

点赞