5

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);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
stepik21
  • 2,610
  • 3
  • 22
  • 32

1 Answers1

2

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Ohh so I need to store every of that device tokens in database and than I must array them and send that yep? Can you explain please how can I send devicetoken to my server from xcode? Thx very much, Steve – stepik21 Apr 13 '13 at 08:18
  • @stepik21 What do you mean by sending the device token from xcode? You have to write code in the application that contacts your server and sends the device token. For example, you can send an HTTP POST or HTTP GET request to your server, with the device token as a parameter. – Eran Apr 13 '13 at 11:15
  • Oh yes, I meen from application... Ok, I Will try it :-) – stepik21 Apr 13 '13 at 15:13