Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS Push Notification What am I doing wrong?
    primarykey
    data
    text
    <p>I have tried to send push notifications to my apps multiple times, but with no avail. I have followed many tutorials and followed all the steps. I am getting successful messages from the APNS server, yet no message ever arrives to the device. I have tested sending using the development certificate which arrived successfully, and I have generated the appropriate production certificates and am using them in the php script. I will post my code below. Please let me know if you see something I am doing wrong. I just don't know what it is...</p> <p>Any help would be GREATLY appreciated.</p> <pre><code>&lt;?php $link=mysqli_connect('****','****','****','***'); if(mysqli_connect_errno()) { header('HTTP/1.1 400'); header('Content-type: text/html'); echo 'Connection Error: %s\n',mysqli_connect_error(); exit; } echo "Connected to Database&lt;br /&gt;"; echo "Querying Database&lt;br /&gt;"; switch ($_REQUEST['App']) { case "O2": $query="SELECT Token FROM O2CalculatorPushTokens"; break; case "LZA": $query="SELECT Token FROM LZAPushTokens"; break; case "MorseCode": $query="SELECT Token FROM MorseCodePushTokens"; break; default: echo "Unknown App."; exit; } $result=mysqli_query($link,$query); echo mysqli_num_rows($result)."&lt;br /&gt;"; if ($result==false) { echo "No tokens to send to."; } else { //Set SSL context $ctx = stream_context_create(); echo "Loading SSL Certificate...&lt;br&gt;"; switch($_REQUEST['App']) { case "O2": stream_context_set_option($ctx, 'ssl', 'local_cert', 'SSL/O2CalculatorProductionSSL.pem'); break; case "LZA": stream_context_set_option($ctx, 'ssl', 'local_cert', 'SSL/LandingZoneAssistantProductionSSL.pem'); break; case "MorseCode": stream_context_set_option($ctx, 'ssl', 'local_cert', 'SSL/MorseCodeProductionSSL.pem'); break; default: exit; } echo "Unlocking SSL Certificate...&lt;br&gt;&lt;br&gt;"; stream_context_set_option($ctx, 'ssl', 'passphrase', '****'); //Open a connection to the APNS server echo "Connecting to APNS server...&lt;br&gt;"; $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if(!$fp) exit("Failed to connect: $err $errstr&lt;br&gt;&lt;br&gt;"); else echo "Connected to APNS!&lt;br /&gt;"; //Create the payload body echo "Creating message...&lt;br&gt;"; $body['aps'] = array('alert' =&gt; $_REQUEST['PushMessageTextArea']); echo $body."&lt;br&gt;"; //Encode the payload as JSON echo "Encoding message...&lt;br&gt;"; $payload = json_encode($body); echo $payload."&lt;br&gt;"; while($row=mysqli_fetch_assoc($result)) { //Build the binary notification echo "Sending message to ".$row['Token']."&lt;br&gt;"; $msg = chr(0).pack('n',32).pack('H*',$row['Token']).pack('n',strlen($payload)).$payload; //Send it to the server $PushResult = fwrite($fp, $msg, strlen($msg)); if (!$PushResult) echo "Message not delivered! &lt;br /&gt;"; else echo "Message successfully delivered! &lt;br /&gt;"; }; //Close connection to the server echo "Closing APNS connection...&lt;br&gt;&lt;br&gt;"; fclose($fp); mysqli_free_result($result); } mysqli_close($link); </code></pre> <p>And my iPhone script...</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //Register for push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert)]; return YES; } - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { if (![[NSUserDefaults standardUserDefaults] boolForKey:@"registeredForPush"]) { //Remove spaces and brackets from deviceToken NSString* token = [deviceToken description]; token = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"&lt;&gt;"]]; token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; NSMutableString *params = [[NSMutableString alloc] initWithFormat:@"Token="]; [params appendString:token]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.bluelineapps.net/****.php"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:100.0]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { //Show alert UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you!" message:@"Thank you for registering for updates. Please rate this app in the AppStore after you've had some time to use it. Feedback is welcome and can be sent using the 'Feedback' tab below. Enjoy!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert dismissWithClickedButtonIndex:0 animated:TRUE]; [alert show]; } else { //Show Error UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration error!" message:@"Failed to register for updates. Please try again later in your 'Settings' app. Sorry for the inconvenience." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert dismissWithClickedButtonIndex:0 animated:TRUE]; [alert show]; } [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"registeredForPush"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"Failed to get token, error: %@", error); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Landing Zone Assistant" message:[userInfo valueForKeyPath:@"alert"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert show]; } @end </code></pre> <hr> <p><strong>UPDATE:</strong></p> <p>Ok, so after deleting all the Mobile Provisioning profiles, all certificates, all .pem files, everything, and re-generating all certs, keys, permissions, and profiles for both development and production my problem still remains...</p> <p>Sent message to debug version on device using the sandbox gateway, successful. Sent message to release version on Ad Hoc version on test device using main apple gateway, unsuccessful....</p> <p>Any ideas?</p> <p>I receive no error messages from the APNS service, everything successful. No build errors. Brand new up-to-date certificates...</p> <hr> <p><strong>UPDATE:</strong></p> <p>I found this article from Apple Technical support <a href="https://developer.apple.com/library/ios/#technotes/tn2265/_index.html#//apple_ref/doc/uid/DTS40010376" rel="nofollow">here</a> and followed all the steps and checked everything. As far as I can tell everything checks out. My production build does contain the aps_environment of Production as it should.</p> <p>Also I found <a href="http://code.google.com/p/apns-php/wiki/CertificateCreation" rel="nofollow">this method</a> for generating the .pem files, which was a little different then my previous attempts, so I tried this, and it still doesn't work. My code is still the same as above as the problem seems to be elsewhere, but if anyone sees something when scanning through PLEASE let me know. I just want this to work.</p> <p>I would happily put a bounty on this question, but I don't have anywhere near enough reputation for it, and I have been trying to gain some by helping others with their questions. </p> <hr> <p><strong>UPDATE:</strong></p> <p>While searching I found another document by Apple <a href="http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html" rel="nofollow">here</a> that has a line in it that states "Take note that the device token in the production environment and the device token in the development (sandbox) environment are not the same value." So I suppose another question would be.</p> <p><strong>Is the deviceToken different for development and production modes?</strong></p> <hr> <p><strong>UPDATE:</strong></p> <p>I know this is getting long but I am trying to show what steps I have taken to try to self solve this problem.</p> <p>I keep seeing this Entrust CA certificate popping up in the documentation. I have used this to verify a connection with the APNS gateway, but I am not using it in my connection while sending push messages because none of the examples I have seen show to use it. Is this required and if so then how do I use it?</p> <hr> <p><strong>UPDATE:</strong></p> <p>I have decided to try using PhoneGap to re-develop my apps and reach additional audiences, so I will close this for now. Thanks everyone for your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload