Write a Push Notification for the iPhone application and send a Notification to the server.

Source: Internet
Author: User

IPhoneWrite an applicationPush Notification ServerThe client sends notifications as described in this article.Push notificationAfter obtaining the device token from the device token, you must send the token string to the application'sServerEnd, that is, provider.

The provider sends the token number, notification content, and notification form (for example, whether a prompt window is displayed or whether a voice is voiced) to the apns server of apple ).

The simplest provider implementation is to establish a secure connection (tsl or ssl) with the Apple Server through a certificate. After a connection is established through authentication, the server sends data streams meeting Apple's requirements.

Obtain certificate

Apple provides two access methods:

Developer for testing

Production, used for products

For internal testing, use developer.

Download the certificate through the ios provisioning portal:

This requirement:

The logon apple developer program account must be the highest-level agent. This is for enterprise accounts. If it is a personal account, it doesn't matter.) The agent account is the initial account; otherwise, the configure link cannot be found;
The developer and product must be enabled after the configure operation.

Go to the configure link and click download:

Process Certificate

If you are writing an objc program running on a mac, skip this step without processing the certificate.

If it is used in java, You need to merge and export the private key used for the certificate and the certificate supporting the notification mentioned above, not the iphone developer certificate.

Generate a certificate:

When you click storage, a file Password is prompted:

Of course, the password can be blank.

The following message is displayed:

Enter the password of the mac logon user.

File generation.

Write an instance for sending a notification

For mac code writing, there is a ready-made project available: http://stefan.hafeneger.name/download/PushMeBabySource.zip

To import to xcode, you only need:

Set deviceToken to the token string of the device. In addition, change pathForResource:

 
 
  1. aps_developer_identity  

In addition, copy the certificate downloaded in the obtained certificate step to the xcode project Resources directory:

We can see that the file name is consistent with the above pathForResource parameter.

Then run the program to receive the push notification on the device.

If it is written in java, you can use a third-party library, see:

Http://code.google.com/p/javapns/

Write a simple notification sending code:

 
 
  1. Import org. json. JSONException;
  2.  
  3. Import javapns. back. PushNotificationManager;
  4. Import javapns. back. SSLConnectionHelper;
  5. Import javapns. data. Device;
  6. Import javapns. data. PayLoad;
  7.  
  8. Public class Main {
  9.  
  10. /**
  11. * @ Param args
  12. * @ Throws Exception
  13. */
  14. Public static void main (String [] args) throws Exception {
  15. PayLoad simplePayLoad = new PayLoad ();
  16. // Get PushNotification Instance
  17. PushNotificationManager pushManager = PushNotificationManager. getInstance ();
  18. // Link the iPhone's UDID (64-char device token) to a stringName
  19. PushManager. addDevice (& quot; iPhone & quot;, & quot; 00000000 00000000 00000000 00000000 00000000 00000000 00000000 & quot ;);
  20. SimplePayLoad. addAlert (& quot; My alert message Test & quot ;);
  21. SimplePayLoad. addBadge (1 );
  22. SimplePayLoad. addSound (& quot; default & quot ;);
  23. Device client = PushNotificationManager. getInstance (). getDevice (& quot; iPhone & quot ;);
  24. PushNotificationManager. getInstance (). initializeConnection (& quot; gateway.sandbox.push.apple.com & quot ;,
  25. 2195, & quot;/home/ubuntu/mypush. p12 & quot;, & quot; password & quot;, SSLConnectionHelper. KEYSTORE_TYPE_PKCS12 );
  26. PushNotificationManager. getInstance (). sendNotification (client, simplePayLoad );

No Chinese characters in the test are garbled.

Compile a complex example to control whether a notification has a prompt window or a reminder sound ):

APayload. addBadge (2), number displayed on the mobile app icon

APayload. addAlert (& quot; software version updates & quot;). The prompt text is displayed.

APayload.addSound("default.wav & quot;) to specify the sound.

You can also use a third-party php implementation, for example:

Http://code.google.com/p/php-apns

The basic principle is to start a php service and monitor the memcacheq queue. If there is a message, it is sent to the Apple Server.

Summary:IPhoneWrite an applicationPush Notification ServerThis article is helpful to you.

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.