IOS Detail Message Push

Source: Internet
Author: User
Tags ssl certificate

After a lot of classmates ask us, how to push on iOS how to do ah, why my device is not receiving push, here with you focus on the implementation of the push on iOS details. push mechanism of APNsUnlike our own push service on Android, Apple controls the device very strictly, and the message push process must be APNs: Here Provider is the developer of an app, and of course if the developer uses AVOs cloud services, Entrust us with the request to send the message, then the provider here is AVOs Cloud's push service program. Can be divided into three steps: The first step: AVOs Cloud push Service program to send the message, the purpose of the device's unique identity package, sent to APNs. Step two: APNs in its list of applications for the registered push service, find the device with the appropriate identity and send the message to the device. The third step: The iOS system sends the message to the appropriate application, and according to the set popup push notification in order to achieve message push, there are two points is very important: 1,app's push certificateTo be able to fully implement a message push, we need to open the push notifications in the app ID, we need to prepare provisioning profile and SSL certificate, And be sure to note that development and distribution environments need to be separated. Finally, import the SSL certificate into the AVOs cloud platform and try to push the remote message. Specific operating procedures can be found in our User guide: iOS push certificate Setup Guide. 2, device identification DevicetokenKnowing who to push, or which app to push to, APNs also needs to know which device to push to, which is what device identification does. The process for obtaining the device identification is as follows: First step: The app opens the push switch and the user wants to confirm that TA wants to get the app's push message The second step: The app gets a devicetoken third step: The app saves Devicetoken, and here is the [ Avinstallation Saveinbackground] Save Devicetoken to AVOs Cloud step fourth: When certain events occur, the developer delegates AVOs Cloud to send push messages, and AVOs The cloud's push server sends a push message to APNs, APNs the last message to the user's device several concepts related to push delivery Message TypeA message pushed over, can have the following forms of expression: 1. Show an alert or banner to show specific content 2. Prompts a new message number 3 on the app icon. Play a sound the developer can set it up at each push, and the developer can choose a different way of prompting when it is pushed to the user's device. Local Message NotificationThere are two kinds of message notifications on iOS, one for local Notification and one for remote messages (Push Notification, also known as Remote Notification), designed both to alert users Now something new has happened that will entice the user to reopen the app. When is local news useful? For example, if you are doing a to-do tool application, there will be a point in time for each item that the user joins, and the user can ask the To-do app to remind Ta at a certain point in time before the event expires. To achieve this, the app can dispatch a local notification to send an alert message or other hint after the time point is reached. When we are dealing with push messages, we can also use these two methods in a comprehensive manner. how to implement push in code first, we want to get devicetoken. The app needs to register for remote notifications every time it starts--by calling UIApplication's Registerforremotenotificationtypes: Method and passing it to the message type parameters that you want to support, such as:
    1. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
    2. {
    3. //Do some initiale working
    4. ...
    5. [Application Registerforremotenotificationtypes:uiremotenotificationtypebadge | Uiremotenotificationtypealert | Uiremotenotificationtypesound];
    6. return YES;
    7. }
If the registration is successful, the Token,ios system that APNs will return to your device will pass it to the app delegate proxy--application: Didregisterforremotenotificationswithdevicetoken: Method, you should save tokens to AVOs cloud backstage in this method, for example:
    1. -(void) Application: (UIApplication *) app Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {
    2. NSLog (@"Receive devicetoken:%@", Devicetoken);
    3. Avinstallation *currentinstallation = [Avinstallation currentinstallation];
    4. [Currentinstallation Setdevicetokenfromdata:devicetoken];
    5. [Currentinstallation Saveinbackground];
    6. }
If the registration fails, the Application:didfailtoregisterforremotenotificationswitherror: method is called and you can see the specific error message through the Nserror parameter, for example:
    1. -(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) Error {
    2. NSLog (@"registration failed, unable to get device ID, specific error:%@", error);
    3. }
Note: The registration process needs to be invoked every time the app is launched, which does not pose an additional burden because the iOS operating system caches the device token locally after the first time it is available, The app then calls Registerforremotenotificationtypes: it returns immediately, and no more network requests are made. In addition, the app layer should not cache device tokens because device tokens can also change-if the user re-installed the operating system, then the device token given by APNs will be different from the previous one, or, The user restore the original backup to the new device, then the original devices token will also expire. second, we're going to handle the callback after the message is received.We can imagine several usage scenarios for message notifications: 1, when the app was not launched, a message notification was received. The operating system then displays an alert message by default, marking a number on the app icon and even playing a sound. 2, after the user sees the message, click on the Action button or click on the app icon. If the action button is clicked, the system launches the application by calling Application:didfinishlaunchingwithoptions: The proxy method, and the notification payload data is passed in. If the app icon is clicked, the system will also call Application:didfinishlaunchingwithoptions: This proxy method to launch the application, the only difference is that there is no notification information in the startup parameters. The sample code is as follows:
  1. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
  2. {
  3. //Do initializing works
  4. ...
  5. if (launchoptions) {
  6. //do something else
  7. ...
  8. [Avanalytics trackappopenedwithlaunchoptions:launchoptions];
  9. }
  10. [Application Registerforremotenotificationtypes:uiremotenotificationtypebadge | Uiremotenotificationtypealert | Uiremotenotificationtypesound];
  11. return YES;
  12. }
3, what happens when the app is running when the remote message is sent over? The application:didreceiveremotenotification of the app Agent: The method is called, and the payload data in the remote message is passed in as a parameter. The sample code is as follows:
  1. -(void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo {
  2. if (application.applicationstate = = uiapplicationstateactive) {
  3. //Convert to a local notification, display to the notification bar, you can also directly display a alertview, just that slightly aggressive:)
  4. Uilocalnotification *localnotification = [[Uilocalnotification alloc] init];
  5. Localnotification.userinfo = UserInfo;
  6. Localnotification.soundname = Uilocalnotificationdefaultsoundname;
  7. Localnotification.alertbody = [[UserInfo objectforkey:@"APS"] objectforkey:@"alert"];
  8. Localnotification.firedate = [NSDate Date];
  9. [[UIApplication sharedapplication] schedulelocalnotification:localnotification];
  10. } Else {
  11. [Avanalytics Trackappopenedwithremotenotificationpayload:userinfo];
  12. }
  13. }
FAQs FAQ 1. Can I send long messages? No, APNs limits the maximum length of payload per notification to 256 bytes, and the extra-long message cannot be sent. 2. How do I add a sound reminder to my push? A message push is a sound that can be specified. For example, you can use a cheerful voice for positive feedback, and a low-pitched sound for negative feedback, all of which can be used to make a person's eyes shine. You need to put some AIFF, WAV, or CAF audio files into the app's resource file, and then specify a different audio file name when you push. 3. What is the badge of the push? Push does not necessarily cause the red number on the app icon to increase, and whether or not to display this number, depends on the developer himself. When sending a push message, we can choose whether or not to increment this number, and if you do not select this item, then the message push does not cause the red number on the app icon to appear. OK, now the question is coming, if this number is out, how can we let it go away? In fact, we just need to set the Uiapplication.applicationiconbadgenumber property at 0 at any time to let this number go away. In general we will choose when the application starts (Application:didfinishlaunchingwithoptions: method), or simply, Each time the app is switched to the foreground (Applicationwillenterforeground: method), this line of code is called to erase the badge number immediately. 4. What is the notification format that AVOs cloud platform sends out?For each push message, there is a payload, usually composed of a JSON dictionary, which is essential to the APS attribute, which corresponds to the value of a dictionary, Contains some of the following: 1) alert message (text or dictionary) 2) application icon on the Red Number 3) play the sound file name in the app opened by push activation event, application: Didfinishlaunchingwithoptions: The options parameter is the big Dictionary object.
    1. {
    2. APS = {
    3. alert = "Hello, everyone";
    4. badge = 2;
    5. Sound = default;
    6. };
    7. }
Notice here that the value of the alert section can be either a string (text message) or a JSON dictionary. When it is a text message, the system will display the text in a alertview, if it is also composed of a JSON dictionary, its format is as follows: * body* action-loc-key* loc-key* loc-args* Launch-image body part is alertview in the text message to be displayed, loc-property is mainly used to implement localization message, Launch-image is just the name of an image file in the main bundle of the app, in general we do not specify this property. 5. How to display localized messagesThere are two ways to localize push messages: 1) Use Loc-key and Loc-args in the push payload to specify localization, so that the provider party only needs to be sent in a uniform format, and the parsing and assembly of messages is done by the client. 2) If the push payload contains no Loc-key/loc-args information, then the provider party needs to do local processing, and then send different messages to different device, in order to do this, but also need the app to upload device Token when the user's language settings are also transmitted back. At present, because AVOs cloud is mainly aimed at Chinese mainland market and overseas Chinese users, so we do not provide multi-lingual support on the push. 6. How the app responds to push messagesThe process described above can only be a simple display of remote messages, activating the user to get them back to the app. But sometimes we want to give users a better experience, such as if we tell the user: Zhang San just commented on your photo. At this time, if the user clicks the action button to enter the app, do we show the specific comment page as well, or display the usual launch page and let the user find Zhang San's comments on their own? I think responsible developers will choose the former. To be flexible in responding to different types of notification messages, we need to add more information to the payload of the notification, rather than just the text message with alert. For the AVOs cloud messaging push platform, developers are required to use the JSON format for more advanced features. For example, we send this JSON string {"action": {"type": 4}, "alert": "Hello, Everyone"} will eventually receive such userinfo Dictionary in the app:
    1. {
    2. Action = {
    3. Type = 4;
    4. };
    5. APS = {
    6. alert = "Hello, everyone";
    7. badge = 4;
    8. };
    9. }
"Hello, everyone" is displayed in Alertview, but the entire dictionary is passed launchoptions to Application:didfinishlaunchingwithoptions: method, This allows us to implement different jumps for different messages within the program. Link Address: http://www.cocoachina.com/industry/20140528/8582.html

IOS Detail Message Push

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.