The specific implementation of Apple APNs in the app

Source: Internet
Author: User
Tags ssl certificate
<span id="Label3"></p><p>In view of the difficult development of server stability, small teams do not recommend their own development, the proposed use of stable third-party push programs, such as a push, butterflies and so On.<br>To use the Apple APNs push message, first of all, the development app's Xcode used to upload the certificate to the server, of course, your certificate to use a hot certificate or check the push option of the publisher, the General developer certificate is not receiving the push Message.<br>Install the certificate to the service side<br>You should install the SSL certificate and the private key to your provider program running on the Server.<br>The steps are as Follows:<br>0. Install the certificate to the keychain of the Mac Computer.<br>1. Open the Keychain and click on the My Certificate bar on the left Panel.<br>2. Locate the SSL certificate and expand to see the certificate and private Key.<br>3. We select the certificate and the private key and export it as "personal information interchange file" – that is, a file with the extension p12.<br>4.provider server programs are best used in languages such as Ruby and Perl to facilitate the processing of certificates in "personal information interchange files". Open Terminal under Mac Enter the following command to convert the certificate into a format that is ready for communication in such a language:<br>OpenSSL pkcs12-in certificatename.p12-out certificatename.pem-nodes<br>5. Copy the Pem file to the server and install it in the appropriate Location.<br>Finish the service end of the specific client bar, first in the APPDELEGATE.M (appdelegate.mm) file-(BOOL) application: (uiapplication<em><em>) application Didfinishlaunchingwithoptions: (nsdictionary</em></em>Launchoptions add [appdelegate registerforremotenotification] to regain device-related tokens, do not cache Tokens.<br>When logging off, This account is logged on to another device (kicked Off) or captured to a Pull-off event (-(void) applicationwillterminate: (uiapplication *) Application) need to cancel the push registration, code such as [[ UIApplication sharedapplication] unregisterforremotenotifications];//after The user exits the login, cancel the push registration, log in register, of course, after exiting to the landing page after the successful landing will need to re-push the Registration.<br>In the didreceiveremotenotification can handle received messages, can only be recorded into the global variables temporarily do not operate, you can play ringtones, vibrations, pop-up dialog boxes, jump pages and so On. Push message processing like this version update does not tell the user if ([type isequaltostring:@ "psy_needupgrade"])<br>{<br>NSString *url = [page objectforkey:@ "downloadurl"];<br>If (url! = Nil)<br>{<br>G_needupgrade = 1;<br>G_downloadurl = url;<br>}<br>Return<br>}<br>The following code is the processing of the received push Message.</p><pre class="prettyprint"><code class=" hljs haskell">-(void) Application: (<span class="hljs-type"><span class="hljs-type">uiapplication</span></span>*) Application Didreceiveremotenotification: (<span class="hljs-type"><span class="hljs-type">nsdictionary</span></span>*) userInfo fetchcompletionhandler: (void (^) (<span class="hljs-type"><span class="hljs-type">Uibackgroundfetchresult</span></span>)) completionhandler{<span class="hljs-type"><span class="hljs-type">Flddlogdebug</span></span>(@<span class="hljs-string"><span class="hljs-string">"push userinfo:%@"</span></span>, userInfo);<span class="hljs-type"><span class="hljs-type">nsdictionary</span></span>*aps = [userInfo objectforkey:@<span class="hljs-string"><span class="hljs-string">"aps"</span></span>];<span class="hljs-type"><span class="hljs-type">Nsinteger</span></span>Count = [[aps objectforkey:@<span class="hljs-string"><span class="hljs-string">"badge"</span></span>] toint]; [application setapplicationiconbadgenumber:count];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*alert = [aps objectforkey:@<span class="hljs-string"><span class="hljs-string">"alert"</span></span>];<span class="hljs-type"><span class="hljs-type">nsdictionary</span></span>*page = [userInfo objectforkey:@<span class="hljs-string"><span class="hljs-string">"page"</span></span>];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*actionid = [page objectforkey:@<span class="hljs-string"><span class="hljs-string">"id"</span></span>];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">Type</span> = [page objectforkey:@ "<span class="hljs-keyword">type</span>"];</span></span> <span class="hljs-type"><span class="hljs-type">NSString</span></span>*title = [page objectforkey:@<span class="hljs-string"><span class="hljs-string">"title"</span></span>];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*notifytype = [[page objectforkey:@<span class="hljs-string"><span class="hljs-string">"notifytype"</span></span>] toString];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*subtype = [[page objectforkey:@<span class="hljs-string"><span class="hljs-string">"subtype"</span></span>] toString];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*subid = [[page objectforkey:@<span class="hljs-string"><span class="hljs-string">"subId"</span></span>] Tostring];//app message corresponding to the order ID<span class="hljs-type"><span class="hljs-type">NSString</span></span>*phone = [page objectforkey:@<span class="hljs-string"><span class="hljs-string">"usertel"</span></span>];<span class="hljs-type"><span class="hljs-type">NSString</span></span>*userphone = [<span class="hljs-type"><span class="hljs-type">User</span></span>currentuser].phone;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(! [phone Isequaltostring:userphone]) {return; }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span> isequaltostring:@ "psy_needupgrade"])</span></span>{<span class="hljs-type"><span class="hljs-type">NSString</span></span>*url = [page objectforkey:@<span class="hljs-string"><span class="hljs-string">"downloadurl"</span></span>];<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(url! = Nil) {g_needupgrade =<span class="hljs-number"><span class="hljs-number">1</span></span>; G_downloadurl = url; } return; }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([notifytype isequaltostring:@<span class="hljs-string"><span class="hljs-string">"1"</span></span>]) {<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">Type</span> = kfhlappnotify;</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([notifytype isequaltostring:@<span class="hljs-string"><span class="hljs-string">"2"</span></span>]){<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">Type</span> = kfhlordernotify;</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span> isequaltostring:kfhlgrab]) <span class="hljs-container">{//<span class="hljs-title">set</span> <span class="hljs-title">home</span> <span class="hljs-title">refresh</span> <span class="hljs-title">tag</span> [[<span class="hljs-type">nsnotificationcenter</span> <span class="hljs-title">Default Center</span>] <span class="hljs-title">postnotificationname</span>:<span class="hljs-type">refresh_home_notification</span> <span class="hljs-title">object</span>:<span class="hljs-title">nil</span> ]; }</span></span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(application.applicationstate = =<span class="hljs-type"><span class="hljs-type">uiapplicationstateactive</span></span>) {[application Setapplicationiconbadgenumber:<span class="hljs-number"><span class="hljs-number">0</span></span>];<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-type"><span class="hljs-type">AppManager</span></span>boolvalueforkey:@<span class="hljs-string"><span class="hljs-string">"shock"</span></span>]) {<span class="hljs-type"><span class="hljs-type">Audioservicesplaysystemsound</span></span>(ksystemsoundid_vibrate); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{[self playaudiowithindex:<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span>];</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-keyword"><span class="hljs-keyword">type</span></span>Isequaltostring:kfhllogout])<span class="hljs-container">{<span class="hljs-title"><span class="hljs-title">G_loginstat</span></span>=<span class="hljs-type"><span class="hljs-type">Login_state_exit_login</span></span>; // [<span class="hljs-type"><span class="hljs-type">AppManager</span></span> <span class="hljs-title"><span class="hljs-title">Savecurrentorderremind</span></span>]; [[<span class="hljs-type"><span class="hljs-type">uiapplication</span></span> <span class="hljs-title"><span class="hljs-title">sharedapplication</span></span>]<span class="hljs-title"><span class="hljs-title">unregisterforremotenotifications</span></span>]; [[<span class="hljs-type"><span class="hljs-type">User</span></span> <span class="hljs-title"><span class="hljs-title">CurrentUser</span></span>]<span class="hljs-title"><span class="hljs-title">Removeuserinfo</span></span>]; [<span class="hljs-type"><span class="hljs-type">AppManager</span></span> <span class="hljs-title"><span class="hljs-title">Setuserdefaultsvalue</span></span>:@""<span class="hljs-title"><span class="hljs-title">Key</span></span>:@"<span class="hljs-title"><span class="hljs-title">Telephone</span></span>"]; [<span class="hljs-type"><span class="hljs-type">AppManager</span></span> <span class="hljs-title"><span class="hljs-title">Setuserdefaultsvalue</span></span>:@""<span class="hljs-title"><span class="hljs-title">Key</span></span>:@"<span class="hljs-title"><span class="hljs-title">Password</span></span>"];<span class="hljs-type"><span class="hljs-type">Uialertview</span></span>*<span class="hljs-title"><span class="hljs-title">Alertview</span></span>= [[<span class="hljs-type"><span class="hljs-type">Uialertview</span></span> <span class="hljs-title"><span class="hljs-title">Alloc</span></span>]<span class="hljs-title"><span class="hljs-title">Initwithtitle</span></span>:<span class="hljs-title"><span class="hljs-title">Nil</span></span> <span class="hljs-title"><span class="hljs-title">message</span></span>:<span class="hljs-title"><span class="hljs-title">Alert</span></span> <span class="hljs-title"><span class="hljs-title">Delegate</span></span>:<span class="hljs-title"><span class="hljs-title"></span> self</span> <span class="hljs-title"><span class="hljs-title">Cancelbuttontitle</span></span>: @ "ok"<span class="hljs-title"><span class="hljs-title">Otherbuttontitles</span></span>:<span class="hljs-title"><span class="hljs-title">Nil</span></span>,<span class="hljs-title"><span class="hljs-title">Nil</span></span>];<span class="hljs-title"><span class="hljs-title">Alertview</span></span>.<span class="hljs-title"><span class="hljs-title">Tag</span></span>= 1005; [<span class="hljs-title"><span class="hljs-title">Alertview</span></span> <span class="hljs-title"><span class="hljs-title">Show</span></span>]; }</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span> isequaltostring:kfhlgrab]) <span class="hljs-container">{//<span class="hljs-title">set</span> <span class="hljs-title">home</span> <span class="hljs-title">refresh</span> <span class="hljs-title">tag</span> //[<span class="hljs-type">AppManager</span> <span class="hljs-title">setuserboolvalue</span>:<span class="hljs-type">YES</span> <span class="hljs-title">key</span>: @ "<span class="hljs-type">needrefreshhome</span>"]; }</span></span></span> <span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-keyword"><span class="hljs-keyword">type</span></span>isequaltostring:kfhlsend] | | [<span class="hljs-keyword"><span class="hljs-keyword">type</span></span>Isequaltostring:kfhlreceived])<span class="hljs-container"><span class="hljs-container">{// <span class="hljs-type">Order</span> *<span class="hljs-title">Order</span> = [[<span class="hljs-type">order</span> <span class="hljs-title">alloc</span>] <span class="hljs-title">init</span>]; <span class="hljs-title">order</span>. <span class="hljs-title">id</span> = <span class="hljs-title">actionid</span>; [[<span class="hljs-type">nsnotificationcenter</span> <span class="hljs-title">defaultcenter</span>] <span class="hljs-title">postnotificationname</span>:<span class="hljs-type">refresh_order _notification</span> <span class="hljs-title">Object</span>:<span class="hljs-title">nil</span> <span class="hljs-title">userInfo</span>: @{@ "<span class="hljs-type">order</span>": <span class="hljs-title">order</span>, @ "<span class="hljs-type">Option</span>": @ (3)}</span></span>];</span>}<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span> isequaltostring:kfhlbeappoint]) <span class="hljs-container">{ <span class="hljs-type"></span> order *<span class="hljs-title">Order</span> = [[<span class="hljs-type">order</span> <span class="hljs-title">alloc</span>] <span class="hljs-title">init</span>]; <span class="hljs-title">order</span>. <span class="hljs-title">id</span> = <span class="hljs-title">subId</span>; [[<span class="hljs-type">nsnotificationcenter</span> <span class="hljs-title">defaultcenter</span>] <span class="hljs-title">postnotificationname</span>:<span class="hljs-type">refresh_order_ NOTIFICATION</span> <span class="hljs-title">Object</span>:<span class="hljs-title">nil</span> <span class="hljs-title">userInfo</span>: @{@ "<span class="hljs-type">order</span>": <span class="hljs-title">order</span>, @ "<span class="hljs-type">Option</span>": @ (3)}</span>];</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([subtype isequaltostring:kfhlsubclosed] | | [subtype isequaltostring:kfhlsubrejected]) {<span class="hljs-type"><span class="hljs-type">Order</span></span>*order = [[<span class="hljs-type"><span class="hljs-type">Order</span></span>alloc] init]; Order.id = subId;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([subtype isequaltostring:kfhlsubrejected]) {order.state = @<span class="hljs-string"><span class="hljs-string">"</span> the"</span>; } [[<span class="hljs-type"><span class="hljs-type">Nsnotificationcenter</span></span>defaultcenter] postnotificationname:<span class="hljs-type"><span class="hljs-type">refresh_order_notification</span></span>Object:nil userinfo:@{@<span class="hljs-string"><span class="hljs-string">"Order"</span></span>: order, @<span class="hljs-string"><span class="hljs-string">"Option"</span></span>: @(<span class="hljs-number"><span class="hljs-number">3</span></span>)}]; }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>([<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span> isequaltostring:kfhlcancel]) <span class="hljs-container">{ <span class="hljs-type"></span> order *<span class="hljs-title">Order</span> = [[<span class="hljs-type">order</span> <span class="hljs-title">alloc</span>] <span class="hljs-title">init</span>]; <span class="hljs-title">order</span>. <span class="hljs-title">id</span> = <span class="hljs-title">actionid</span>; [[<span class="hljs-type">nsnotificationcenter</span> <span class="hljs-title">defaultcenter</span>] <span class="hljs-title">postnotificationname</span>:<span class="hljs-type">refresh_order_ NOTIFICATION</span> <span class="hljs-title">Object</span>:<span class="hljs-title">nil</span> <span class="hljs-title">userInfo</span>: @{@ "<span class="hljs-type">order</span>": <span class="hljs-title">order</span>, @ "<span class="hljs-type">Option</span>": @ (4)}</span>];</span></span>}<span class="hljs-type"><span class="hljs-type">Uialertview</span></span>*alertview = [[<span class="hljs-type"><span class="hljs-type">Uialertview</span></span>alloc] Initwithtitle:title Message:alert Delegate:self Cancelbutto ntitle:@<span class="hljs-string"><span class="hljs-string">"ignore"</span></span>otherbuttontitles:@<span class="hljs-string"><span class="hljs-string">"enter"</span></span>, nil];<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span>. length > 0 && actionid.length > 0) <span class="hljs-container">{ <span class="hljs-title">objc_setassociatedobject</span>(<span class="hljs-title">alertview</span>, &<span class="hljs-type">alertassociatedkey</span>, @{@ "<span class="hljs-title">type <!-- c6--> ": <span class="hljs-title">type</span>, @"<span class="hljs-title">actionid</span>": <span class="hljs-title">ActionId</span>}, <span class="hljs-type">objc_association_retain_nonatomic</span>);</span></span></span></span>} [alertview show]; } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(application.applicationstate = =<span class="hljs-type"><span class="hljs-type">uiapplicationstateinactive</span></span>) {[self Pushviewcontrollerwithtype:<span class="hljs-typedef"><span class="hljs-typedef"> <span class="hljs-keyword">type</span> actionid:actionid];</span></span>}}</code></pre><p><p>The following code is a specific push registration:</p></p><p><p>"'<br>+ (void) Registerforremotenotification {</p></p><pre><pre><code>FLDDLogDebug(@"*\n*\n*\nregisterForRemoteNotification\n*\n*\n*\n");if (IOS8_OR_LATER) { UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeNewsstandContentAvailability; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];} else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeNewsstandContentAvailability)];}</code></pre></pre><p><p>}</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>The specific implementation of Apple APNs in the app</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.