Fundamentals of iOS Push messages
Last Update:2015-10-15
Source: Internet
Author: User
<span id="Label3"></p><p style="color: rgb(51, 51, 51); font-size: 14px; white-space: normal; margin-top: 10px; margin-right: auto; margin-left: auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: rgb(255, 255, 255);"><p style="color: rgb(51, 51, 51); font-size: 14px; white-space: normal; margin-top: 10px; margin-right: auto; margin-left: auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: rgb(255, 255, 255);"><span style="font-weight: bold;">The principle of push:</span></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">The working mechanism of Push can be summarized simply as<br></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">In the figure, provider is the push server for an iphone software, which I will use. net as the Provider.<br>APNS is an abbreviation for Apple push Notification Service (apple push server) and is Apple's server.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Can be divided into three stages.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Phase One:. NET Applications package the message to be sent, the identity of the target iphone, and send it to Apns.<br>Phase Ii: APNs in its own list of Iphone's registered push services, look for an iphone with a corresponding logo and send the message to the IPHONE.<br>The third stage: the iphone sends the message to the appropriate application and follows the settings to eject the push Notification.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">From what we can See.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1, The first is the application registration message Push.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">2, iOS and APNs server to Devicetoken. The application accepts Devicetoken.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">3. The application sends the Devicetoken to the push service-side Program.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">4. The Service-side program sends messages to the APNs service.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">5. The APNS service sends the message to the iphone app.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Both the iphone client and apns, provider and APNs need to be connected through a Certificate. Let me introduce some of the certificates Used.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>Several certificates:</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>i.. *.certsigningrequest File</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1. Generate certificate Signing Request (CSR):</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">2, fill in your mailbox and common Name, here fill in for Pushchat. Select Save to Hard Disk.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">This generates a <span style="background-color: rgb(255, 0, 0);">pushchat.certsigningrequest</span> file locally.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>second, Generate *.P12 File</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1. Export the key and enter your Password.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Enter your password:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">This generates a <span style="background-color: rgb(255, 0, 0);">pushchatkey.p12</span> file.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>III. Create a new app ID and SSL certificate file</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1. Sign in to iOS Provisioning Portal with your paid Apple Account. Create a new app ID.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Description: Medium Input Pushchat</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Bundle Seed ID: default selection Generate New</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Bundle Identifier: Input Com.mysoft.PushChat</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Click Submit</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">This results in the following record:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Click Configure:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">The following screen appears, click Continue:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Here we select the previously generated pushchat.certsigningrequest file and click Generate.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">is generating</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">After the build, we download it. Name is <span style="background-color: rgb(255, 0, 0);">aps_developer_identity.cer</span>.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Click Done and you will see that the status becomes Enabled.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">So far, we have generated 3 files.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1, Pushchat.certsigningrequest</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">2, PUSHCHATKEY.P12</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">3, Aps_developer_identity.cer</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>Now we create a simple iphone app.</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1. Open Xcode and choose to create a view-based Application. Names such as:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">2. Add the following code to the Didfinishlaunchingwithoptions method in Pushchatappdelegate:</p></p> <pre><pre>-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ Self.window.rootViewController = self.viewcontroller; [self.window makekeyandvisible]; Let the device know we want to receive push notifications [[uiapplication sharedapplication] Registerforremotenotifi Cationtypes: (uiremotenotificationtypebadge | Uiremotenotificationtypesound | uiremotenotificationtypealert)]; Return YES;}</pre></pre> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">The Registerforremotenotificationtypes method tells the application that it can accept push notifications.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">3, run in xcode, will pop up the following prompt box:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Select Ok. Indicates that this application turns on the message notification Service.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Add the following method in the PUSHCHATAPPDELEGATE.M code to get the Devicetoken:</p></p> <pre><pre>-(void) application: (uiapplication*) Application didregisterforremotenotificationswithdevicetoken: (NSData*) Devicetoken{NSLog (@ "My token is:%@", devicetoken);}-(void) application: (uiapplication*) application Didfailtoregiste Rforremotenotificationswitherror: (nserror*) error{NSLog (@ "Failed to get token, error:%@", error);}</pre></pre> <p><p>Get to the devicetoken that we can submit to. NET applications through the WebService service, where I simply process it, print it directly, and copy it to A. NET application Environment.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">A. NET application that sends a notification needs to know devicetoken, and requires a certificate that is connected to the Apns.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">This certificate can be obtained from the two files we generated earlier.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Use OpenSSL to generate the certificate files for. net and APNs Communications.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">1. Convert Aps_developer_identity.cer to Aps_developer_identity.pem format.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">OpenSSL x509-in aps_developer_identity.cer-inform der-out aps_developer_identity.pem-outform PEM</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">2, the P12 format of the private key conversion to pem, you need to set 4 times password, where the password is set to: abc123.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">OpenSSL pkcs12-nocerts-out pushchat_noenc.pem-in PUSHCHAT.P12</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">3. Create pkcs#12 format files with certificate and the Key.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">OpenSSL pkcs12-export-in Aps_developer_identity.pem-inkey pushchat_noenc.pem-certfile PushChat.certSigningRequest- Name "aps_developer_identity"-out APS_DEVELOPER_IDENTITY.P12</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">This way we get the certificate file used in The. net application:<span style="background-color: rgb(255, 0, 0);">aps_developer_identity.p12</span>.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>Send notifications in A. Net Application.</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">There is an Open-source class library: Apns-sharp.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">The address is: http://code.google.com/p/apns-sharp/.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">We download the source code, the inside of the JdSoft.Apple.Apns.Notifications to make corresponding adjustments can be Used.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">We adjust the JdSoft.Apple.Apns.Notifications.Test code according to Devicetoken and p12file, for Example.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">This will be Ok.</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>Effect:</strong></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">Code for the Notification:</p></p> <pre>for (int i = 1; i <= count; i++) { //Create a new notification to send Notification Alertnotification = new notification (testdevicetoken); alertnotification.payload.alert.body = string. Format ("testing {0} ...", i); alertNotification.Payload.Sound = "default"; alertnotification.payload.badge = i; //Queue the notification to be sent if ( Service. Queuenotification (ALERTNOTIFICATION)) console.writeline ("notification queued!"); else console.writeline ("notification failed to be queued!"); //sleep in between each message if (i < count) { console.writeline ("sleeping " + sleepBetweenNotifications + " Milliseconds before next notification ... "); system.threading.thread.sleep ( sleepbetweennotifications); } }</pre> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;">The display on the ipad that was taken on the phone:</p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"></p></p> <p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><p style="margin-top: 10px; margin-right: auto; margin-left: auto;"><strong>summary:</strong> This article focuses on the implementation of the iOS messaging push mechanism, and how to send messages to iOS applications Via. net Applications.</p></p> <p><p><br></p></p><p><p>Fundamentals of iOS Push messages</p></p></span>