IOS message push implementation process record

Source: Internet
Author: User

Here we record the whole process of implementing iOS message push.

First, apply for a key.




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + fingerprint + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140416/2014041609101140.png" alt = "\">





The following describes how to create a push certificate.




Connection:



:



Step 1:



Add the push function for the certificate:


Effect after clicking:


After clicking









Now we have two certificates: aps_development.cer and CertificateSigningRequest. certSigningRequest.

The following steps are critical. We need to clean up the previous certificates on the machine. The following error may occur if the image is not cleaned:

Error Domain = NSCocoaErrorDomain Code = 3000"The application's"Aps-environment"Right string"UserInfo = 0x17007d6c0 {NSLocalizedDescription =The application's"Aps-environment"Right string}

This error probably means that the certificate does not match.



After cleaning, click


Add the push certificate.

Then we export a file that can be used by the server.





The following code demonstrates the active contribution of Lei Feng on the Internet.

The client code is attached below:

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. NSString * str = [UIDevice currentDevice]; if ([launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]! = Nil) {// get 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] workflow: UIRemoteNotificationTypeSound | workflow | uiremotenotiftypetypebadge]; return YES;}-(void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {NSString * token = [NSSt Ring stringWithFormat: @ "% @", deviceToken]; NSLog (@ "% @", token);}-(void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {NSString * error_str = [NSString stringWithFormat: @ "% @", error]; NSLog (@ "Failed to get token, error: % @", error_str );} -(void) application :( UIApplication *) applicationdidReceiveRemoteNotification :( NSDictionary *) userInfo {// Process the received message here. NSLog (@ "Receive remote notification: % @", userInfo );}









The server code is attached below:

Import org. apache. log4j. logger; import org. apache. log4j. propertyConfigurator; import javapns. *; import javapns. back. pushNotificationManager; import javapns. back. SSLConnectionHelper; import javapns. data. device; import javapns. data. payLoad; public class PushDemo {public static void main (String [] args) throws Exception {try {// deviceToken obtained from the client, in order to make the test simple, write a fixed device ID. String deviceToken = "9f8e0099 1e72c4ab ca835ec3 994e93d9 a09ce215 62d91bb3 6e929a33 02c29456"; 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 = "/Users/tokou/Desktop/PushDemo. p12 "; // The previously generated JAVA backend connection to the APNS Service *. p12 File Location String certificatePas Sword = "123"; // 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 ();}}}


















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.