IOS local, APNS remote push (specific process), iosapns push

Source: Internet
Author: User
Tags key string openssl x509 pkcs12

IOS local, APNS remote push (specific process), iosapns push

Add local push

/// Add locally

  1. -(Void) addLocalPushNotification :( UIButton *) sender;
  2. {
  3. NSLog (@ "% s" ,__ FUNCTION __);
  4. UILocalNotification * localNotification = [[UILocalNotification alloc] init];
  5. If (localNotification ){
  6. // Set the current time to 20 seconds
  7. NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow: 20];
  8. /* Push time zone settings: Found online
  9. TimeZone indicates whether the excitation time of UILocalNotification changes according to the time zone. If it is set to nil, UILocalNotification will be triggered after a period of time, rather than a specific time. */
  10. LocalNotification. timeZone = [NSTimeZone defaultTimeZone];
  11. /// Set the push time
  12. LocalNotification. fireDate = pushDate;
  13. // Time interval. If this parameter is not set, only one push will be performed.
  14. LocalNotification. repeatInterval = kCFCalendarUnitDay;
  15. // The push sound. (If this parameter is not set, no sound will appear during the push)
  16. LocalNotification. soundName = uilocalnotificationdefasoundname;
  17. // Push text information (if not set, text is not displayed in the push center. If there is a sound prompt, you must set a sound)
  18. LocalNotification. alertBody = @ "Hello world ";
  19. // Set the push time icon. PS: I don't know how to use this item.
  20. LocalNotification. alertLaunchImage = [[NSBundle mainBundle] pathForResource: @ "3" ofType: @ "jpg"];
  21. /// This item is used to locate which notification is used for cancellation.
  22. NSDictionary * infoDic = [NSDictionary dictionaryWithObject: @ "name" forKey: @ "key"];
  23. LocalNotification. userInfo = infoDic;
  24. // Lecture push settings and add information
  25. UIApplication * app = [UIApplication sharedApplication];
  26. BOOL status = YES;
  27. For (UILocalNotification * notification in app. scheduledlocalconfigurications ){
  28. If ([notification. userInfo objectForKey: @ "key"]) {
  29. Status = NO;
  30. }
  31. }
  32. If (status ){
  33. // Add push (only once)
  34. [App scheduleLocalNotification: localNotification];
  35. }
  36. NSLog (@ "% @", app. scheduledlocalconfigurications );
  37. }
  38. }

 

 

Cancel local push
  1. /// Local Removal
  2. -(Void) removeLocalPushNotification :( UIButton *) sender
  3. {
  4. NSLog (@ "% s" ,__ FUNCTION __);
  5. UIApplication * app = [UIApplication sharedApplication];
  6. // Obtain all notifications of the current application
  7. NSArray * localications ications = [app scheduledLocalNotifications];
  8. If (localconfigurications ){
  9. For (UILocalNotification * notification in localconfigurications ){
  10. NSDictionary * dic = notification. userInfo;
  11. If (dic ){
  12. NSString * key = [dic objectForKey: @ "key"];
  13. If ([key isEqualToString: @ "name"]) {
  14. // Cancel the push (specify a cancellation)
  15. [App cancelLocalNotification: notification];
  16. Break;
  17. }
  18. }
  19. }
  20. }
  21. // Cancel all pushes of the current application
  22. // [App cancelalllocalconfigurications];
  23. }


 

 

 

 

 

Remote push

 

When the server remotely pushes data to APNS to an offline device, the Qos component of the Apple Server automatically keeps the latest notification. After the device goes online, Qos will push data to the target device.

 

Note that

Bundle ID is consistent with App Id

The device Token can be obtained normally.

For sandbox testing, the certificate must use the developer's

 

Single Device

As shown in: Our server submits the information to be pushed to APNS (Apple Push Notification Service). APNS finds the corresponding device in the Push Service IOS device list, push the information to the terminal, and then the terminal to the client APP.

Multiple Devices

 

 

 

The process is probably like this.

1. Generate the CertificateSigningRequest. certSigningRequest File

2. Upload CertificateSigningRequest. certSigningRequest to the developer and export the. cer file.

3. Use CSR to export the P12 File

4. You need to prepare the device token value (no space)

5. Use the push certificate used by the OpenSSL synthesis Server

 

1. Open the key string and select (key string access> certificate assistant> request certificate from the certificate authority) in the upper right corner)

 

Generate CertificateSigningRequest. certSigningRequest

 

Enter the following information and save it to the corresponding location.

 

 

2. Go to pai.apple.com to upload CertificateSigningRequest. certSigningRequest and save the cer file.

(1)

(2) Select the push service type-sandbox Testing

 

(3) Select the corresponding app id. Do not forget that the Bundle ID in the project configuration file is consistent with the corresponding app id.

(4) Select the Save path.

(5) select to upload the file CertificateSigningRequest. certSigningRequest

(6) Save the cer file and double-click it to add the key string

 

(7) create a Provisioning Profiles

 

 

Select the App Id that is consistent with the previous one.

 

Select the newly created certificates

 

Select a debuggable Device

 

Save the provisioning file and add it to the device.

 

Merge through OPENSSL files

1. In the key string-> certificate, right-click the certificate you just added and choose export p12.

2. Enter the terminal and convert aps_development.cer to PushChatCert. pem (openssl x509-in aps_development.cer-inform der-out PushChatCert. pem)

3. openssl pkcs12-nocerts-out PushChatKey. pem-in Push. p12 generate the p12 private key. pem file (you need to set a password for pushing on the server)

4. Use PushChatCert. pem and the newly generated PushChatKey. pem to synthesize a new p12 file (this p12 is provided for server push )(

Openssl pkcs12-export-in PushChatCert. pem-inkey PushChatKey. pem-certfile CertificateSigningRequest. certSigningRequest-name "aps_developer_identity"-out aps_developer_identity.p12

)

 

Merge PEM files used by PHP

 


 

 

 

 

 

The code implementation is as follows:

 

Register push notifications

 

[Cpp]View plaincopy
  1. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
  2. (UIRemoteNotificationTypeAlert |
  3. UIRemoteNotificationTypeBadge |
  4. UIRemoteNotificationTypeSound)];


 

Add the following proxy methods to AppDelegate:

 

[Cpp]View plaincopy
  1. /// This method is used when the Token value is obtained successfully (the Token value cannot contain spaces)
  2. -(Void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken
  3. {
  4. NSLog (@ "% @", deviceToken );
  5. }
  6. /// This method is used when the Token value fails to be obtained.
  7. -(Void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error
  8. {
  9. NSLog (@ "% @", error );
  10. }
  11. /// This method is used when the application is in the open state and the server pushes messages and opens the application through push.
  12. -(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo
  13. {
  14. For (id key in userInfo ){
  15. NSLog (@ "% @: % @", key, [userInfo objectForKey: key]);
  16. }
  17. /// Set the number of Icon pushes to 0
  18. // Application. applicationIconBadgeNumber = 0;
  19. }

If the application is not in the background and opened through push notifications, you can

 

[Cpp]View plaincopy
  1. -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions

Add to Method

 

 

[Cpp]View plaincopy
  1. /// The application is not in the background and opened through push
  2. If (launchOptions ){
  3. /// Obtain push-related information
  4. NSDictionary * userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
  5. }

 

 


 

Server PHP push code

 

[Cpp]View plaincopy
  1. <? Php
  2. $ DeviceToken = 'ba6d5103793c8e62e68b5df1b36c3b58ced1588c6dabe0fc9e6828961aeb12d6 '; // No space
  3. $ Body = array ("aps" => array ("alert" => 'hellohui', "badge" => 2, "sound" => 'default ')); // push method, including content and sound
  4. $ Ctx = stream_context_create ();
  5. // If there is a problem with finding the pem path on a Windows Server, change the path to this method:
  6. // $ Pem = dirname (_ FILE _). '/'. 'apns-dev. pem ';
  7. // Directly write the pem path to the linux server.
  8. Stream_context_set_option ($ ctx, "ssl", "local_cert", "26ck. pem ");
  9. $ Pass = "123123 ";
  10. Stream_context_set_option ($ ctx, 'ssl ', 'passphrase', $ pass );
  11. // There are two servers to choose from. for development and testing, select the server with the second sandbox and use the Dev pem certificate. If it is published, use Product pem and select the official server
  12. // $ Fp = stream_socket_client ("ssl: // gateway.push.apple.com: 2195", $ err, $ errstr, 60, STREAM_CLIENT_CONNECT, $ ctx );
  13. $ Fp = stream_socket_client ("ssl: // gateway.sandbox.push.apple.com: 2195", $ err, $ errstr, 60, STREAM_CLIENT_CONNECT, $ ctx );
  14. If (! $ Fp ){
  15. Echo "Failed to connect $ err $ errstrn ";
  16. Return;
  17. }
  18. Print "Connection OK \ n ";
  19. $ Payload = json_encode ($ body );
  20. $ Msg = chr (0 ). pack ("n", 32 ). pack ("H *", str_replace ('','', $ deviceToken )). pack ("n", strlen ($ payload )). $ payload;
  21. Echo "sending message:". $ payload. "\ n ";
  22. Fwrite ($ fp, $ msg );
  23. Fclose ($ fp);?>
[Php]View plaincopy
  1. <Pre> </pre>
  2. <Pre> </pre>
  3. <Pre> </pre>
  4. <Pre> </pre>
  5. <Pre> </pre>
  6. <Pre> </pre>
  7. <Div style = "padding-top: 20px">
  8. <P style = "font-size: 12px;"> Copyright Disclaimer: This article is the original article of the blogger and cannot be reproduced without the consent of the blogger. </P>
  9. </Div>
 
  • Previous SpriteKit-particle effect-SKEmitterNode
  • Next, convert the first letter of each word in the string to uppercase and use the callback function to implement [function pointer].
 

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.