IOS message push principles and implementation based on APNS (JAVA background), iosapns
Push principle:
The working mechanism of Push can be summarized
In the figure, Provider refers to the Push server of an iPhone software. In this article, I will use. net as the Provider.
APNS is the abbreviation of Apple Push Notification Service (Apple Push server). It is an Apple server.
It can be divided into three stages.
Phase 1: The Push server application packs the message to be sent and the identifier of the target iPhone and sends it to APNS.
Stage 2: APNS searches for the iPhone with the corresponding identifier in its registered Push Service iPhone list and sends the message to the iPhone.
Stage 3: The iPhone transmits the sent message to the corresponding application, and a Push notification is displayed according to the settings.
We can see from.
1. First, the application registers for message push.
2. for IOS and APNS servers, deviceToken is required. The application accepts deviceToken.
3. The application sends the deviceToken to the PUSH server.
4. The server program sends a message to the APNS service.
5. The APNS Service sends messages to the iPhone application.
Both the iPhone client and APNS, Provider and APNS, must be connected through a certificate. The following describes how to create a certificate.
1. CSR File
1. Generate a Certificate Signing Request (CSR)
2. Enter your email address and common name, and save it to the hard disk.
Click to continue:
In this way, a PushTest. certSigningRequest file is generated in the response.
Ii. SSL certificate file
1. log on to the iOS Provisioning Portal with your paid account, and create Certificates (which can be omitted if they have been created), for example:
Click Submit to create Certificate.
2. Create an App ID. Click New App ID, enter Description, Bundle Identifier, and click Submit. The App ID is created.
Find the new App ID and click Configure on the right:
Development Push SSL Certificate, which differs from Production Push SSL Certificate in that one is the Push Certificate for Development and the other is the Push Certificate for publishing products. The device deviceToken obtained by the two certificates is different. The P12 certificate generated by the two certificates is also used to connect the server address of APNS to the JAVA background, the connection server address of the SSL Certificate Push interface is gateway.sandbox.push.apple.com. Production Push SSL Certificate: gateway.push.apple.com.
Click Configure after one line of SSL Certificate pushed by Development: Click Continue:
Select the previously generated PushTest. certSigningRequest file and click Generate. The following page is displayed:
Click Continue:
Click Download to Download the generated certificate that supports PUSH Service (named: aps_development-6.cer ).
Click Done and you will find that the status has changed to Enabled:
So far, we have generated two files:
1. PushTest. certSigningRequest
2. aps_development-6.cer (download the generated certificate that supports PUSH Service .)
Double-click the aps_development-6.cer to register in your key string so that there will be
3. Prepare the profile certificate. Because the PUSH message can only be tested on a real machine, create a profile certificate and click "new profile" to create a profile for the newly created app id, download pushtestdescDevprofile. mobileprovision double-click to add it to xcode's Provisioning Profiles.
4. Generate a JAVA background certificate for connecting to APNS:
Open the key string, select Apple Development IOS Push Services: com. easecom. zhwgpushtestdesc, right-click and export it. Export the P12 certificate used to connect to APNS in the JAVA background. Enter the password of the p12 certificate. In this article, I use 123456. Remember this password, which is used when the JAVA background uses the p12 certificate. Enter the access key string password: The system login password.
The PushTest. p12 certificate has been exported.
So far, we have generated four files:
1. PushTest. certSigningRequest
2. aps_development-6.cer (download the generated certificate that supports PUSH Service .)
3. pushtestdescDevprofile. mobileprovision 4. PushTest. p12
So far, all the IOS message push (JAVA background) certificates have been created.
Start the code below:
V. IOS code:
1. First, add the following two proxy methods to the project's AppDelegate. m:
-(Void) application :( UIApplication *) application didregisterforremotenotifswswithdevicetoken :( NSData *) deviceToken {NSString * token = [NSString stringWithFormat: @ "% @", deviceToken];
// Obtain the terminal device ID. This ID must be sent to the server through the interface. When the server pushes a message to APNS, it must know the terminal ID. APNS finds the terminal device through the registered terminal ID. NSLog (@ "My token is: % @", token );}
-(Void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {NSString * error_str = [NSString stringWithFormat: @ "% @", error]; NSLog (@ "Failed to get token, error: % @", error_str );}
2. In AppDelegate. m (BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) The launchOptions method can be used to register Message notification push capabilities; when the application is not started, determines whether the message is triggered by a remote Message notification; adds the message push notification clearing tag.
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
// Determine whether the application is started by a remote Message notification trigger
If ([launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]! = Nil ){
// Obtain the number of notification tags for the Application message (that is, the number in the red circle)
Int badge = [UIApplication sharedApplication]. applicationIconBadgeNumber; if (badge> 0 ){
// Clear the flag if the number of notification tags in the application message is greater than 0.
Badge --;
// Clear the flag. Clear the number in the red circle. If the number in the Red Circle is 0, the Red Circle is eliminated. [UIApplication sharedApplication]. applicationIconBadgeNumber = badge ;}}
// Message push registration [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeSound | uiremotenotiftypetypealert | uiremotenotiftypetypebadge];
} 3. Add the message receiving and processing proxy method to the project AppDelegate. m.
// Process the received message push-(void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo {
// Process the received message here. NSLog (@ "Receive remote notification: % @", userInfo);} 6. JAVA background code:
Public static void main (String [] args) throws Exception {try {// The deviceToken obtained from the client. To make the test simple, write a fixed device ID. String deviceToken = "df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 109d34ad 20175f8c4"
System. out. println ("Push Start deviceToken:" + deviceToken); // defines the message mode PayLoad payLoad = new PayLoad (); payLoad. addAlert ("this is test! "); PayLoad. addBadge (1); // Number of message push tags, which is displayed in a small red circle. PayLoad. addSound ("default"); // register deviceToken PushNotificationManager pushManager = PushNotificationManager. getInstance (); pushManager. addDevice ("iPhone", deviceToken); // connect to APNS String host = "gateway.sandbox.push.apple.com"; // String host = "gateway.push.apple.com"; int port = 2195;
String certificatePath = "c:/PushTest. p12 "; // The previously generated JAVA backend connection to the APNS Service *. p12 File Location String certificatePassword = "123456"; // p12 File Password. PushManager. initializeConnection (host, port, certificatePath, certificatePassword, SSLConnectionHelper. KEYSTORE_TYPE_PKCS12); // send push Device client = pushManager. getDevice ("iPhone"); System. out. println ("push message:" + client. getToken () + "\ n" + payLoad. toString () + ""); pushManager. sendNotification (client, payLoad); // stop connecting to APNS pushManager. stopConnection (); // Delete deviceToken pushManager. removeDevice ("iPhone"); System. out. println ("Push End");} catch (Exception ex) {ex. printStackTrace ();}}}
The test is successful.
The above is tested on iPhone 4 and IPAD2 devices. For iPhone 3G and 3gs devices, PushDoctor (push doctor) must be installed to pass the test.
Can iOS apps send a local reminder on the background?
1) if the iOS application is active, the client and the server receive and send messages after a persistent connection with the server. 2) If the iOS application exits and the persistent connection is disconnected, the server sends messages to the client through APNS. 3) if the iOS app has just been switched to the backend and has not been closed, the persistent connection is not closed, and the server still sends messages to the client through the persistent connection, however, iOS apps receive messages only when they are switched back to the front-end. In the third case, there is a question:
How to Implement Message push mechanism in ios development
Ios sends messages through the PUSH Service provided by Apple, APNS for short. Developers need to enable the notification service in the App, configure and generate a notification certificate. This certificate must be submitted to Apple for verification. Apple's APNS uses the socket protocol, which is also fixed when submitted to APNS, such as badge, sound, devicetoken ect.