How to get device tokens

I have an iOS app and I want to integrate push notifications there. I looked at the tutorial on youtube and everything is fine, but lately I have been using a development certificate (for testing - not for using the AppStore) and I have a PHP script on my server. This file stores the deviceToken, which has my iPhone, and it is written in the php $ deviceToken variable. But now, when I want to use this in the AppStore, how can I get device tokens from everyone who downloaded my application and get it in a 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); } 
+4
source share
1 answer

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

When the application starts, you must register with Apple Push Notifications by calling the registerForRemoteNotificationTypes: method.

When registration completes successfully and you receive the device token, you must send it to your server in the implementation of application:didRegisterForRemoteNotificationsWithDeviceToken:

Your server should store it in some database.

Your PHP script that sends notifications should receive device tokens from this database.

+2
source

All Articles