How to get device tokens

限于喜欢 提交于 2019-12-10 15:22:18

问题


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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/14102349/how-to-get-device-tokens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!