如何获得设备令牌 [英] How to get device tokens

查看:105
本文介绍了如何获得设备令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序的 iOS版我要整合有推送通知。我在YouTube上看到一个 教程,一切都OK,但最近我使用的是开发证书(用于测试 - 不是AppStore的使用),我有我的服务器上的PHP脚本。在这个文件存储在deviceToken其中有我的iPhone,它是用PHP编写的变量$ deviceToken。但是现在,当我想在AppStore上使用这个,我怎么能得到大家的设备令牌谁曾下载了我的应用程序,并把它搬进PHP脚本?

这是我的PHP文件:


 如果($ _ POST ['消息']){        $ deviceToken ='(我的设备令牌)';        $消息=的stripslashes($ _ POST ['消息']);        $负载='{
                        APS:                            {提醒:'$消息。'
                              徽章:1,
                              声音:bingbong.aiff
                            }
                    };        $ CTX = stream_context_create();
        stream_context_set_option($ CTXSSL,的local_cert',​​'cert.pem');
        stream_context_set_option($ CTX,'SSL','密​​码','密码');
        $计划生育=在stream_socket_client('SSL://gateway.sandbox.push.apple.com:2195',$犯错,$ errstr,60,STREAM_CLIENT_CONNECT,$ CTX);
        如果(!$ FP){
            PRINT无法连接$犯错$ errstrn
            返回;
        }其他{
            打印DONE!;
        }        $ devArray =阵列();
        $ devArray [] = $ deviceToken;        的foreach($ devArray为$ deviceToken){
            $味精= CHR(0)。包(N,32)。包(H *',str_replace函数('','',$ deviceToken))。包(N的strlen($有效载荷))。 $有效载荷;
            FWRITE($ FP,$味精);
        }
        FCLOSE($ FP);
    }<形式的行动=送-notification.php的方法=后>
    <输入类型=文本名称=消息MAXLENGTH =100>
    <输入类型=提交值=SEND>
< /表及GT;
< /身体GT;

这是我在X code(AppDelegate.m)

   - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions
{
    //覆盖点后,应用程序启动定制。
    [UIApplication的sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];    返回YES;
} - (无效)应用:(*的UIApplication)的应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*)deviceToken
{
    * NSString的deviceTokenString = [的NSString stringWithFormat:@%@,deviceToken]
    的NSLog(deviceTokenString);
}


好了,我不知道PHP,所以我不能给你具体的code,但我可以解释的一般原则。

在应用程序启动要注册苹果通过调用 registerForRemoteNotificationTypes推送通知:

在注册成功,你会得到设备令牌,你应该把它发送到您的服务器应用程序的实现:didRegisterForRemoteNotificationsWithDeviceToken:

您的服务器应当将它保存一些数据基础。

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

I have an app for iOS and I want to integrate there push notifications. I have seen a tutorial on youtube and everything is OK, but recently I'm using a development certificate (for testing - not for AppStore use) and I have PHP script on my server. In this file is stored the deviceToken which have my iPhone and it is written in php variable $deviceToken. But now, when I want to use this in AppStore, how can I get the device tokens from everybody who had downloaded my app and get it into the PHP script?

This is my PHP file:

 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>

and this is what I have in 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);
}

解决方案

Well, I don't know PHP, so I can't give you specific code, but I can explain the general principle.

When your application starts you should register to Apple Push Notifications by calling the registerForRemoteNotificationTypes: method.

When the registration succeeds and you get the device token, you should send it to your server in the implementation of application:didRegisterForRemoteNotificationsWithDeviceToken:.

Your server should store it in some data base.

Your PHP script that sends the notifications should get the device tokens from that data base.

这篇关于如何获得设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆