IOS push notification

Source: Internet
Author: User
Tags notification center

Introduction:

Push notification is maintained by mobile devicesConcept of always onlineAs a core way, when people leave the desktop Internet and want to receive the relevant information in the first time, the emergence of push notifications will be even better.

However, pay attention to the need to avoid misuse. A large number of non-essential push messages may cause users to be excited. As part of the user experience, it is already in the direction of failure.

There are two types of push notifications:Local push notificationAndRemote push notification. This article will detail the use of these two types of notifications.

References:

1: IOS local and remote notifications

Http://wangjun.easymorse.com /? P = 1482

2: Example diagram of Apple Remote Notification Service Application activation (written by foreign experts .)

Http://mobiforge.com/developing/story/programming-apple-push-notification-services

3: book reference: push notification in chapter 16th of iPhone development cheats.

Usage:

First, the certificate application URL

Https://developer.apple.com/ios/manage/overview/index.action

After successful logon, go to the IOS Configuration Management Homepage.

Step 1The operation is to create a newAPP IDSAfter the creation is successful, you will need to provide a security certificate to activate the PUSH Service, such:

After you select to store data to a disk, a file name (CSR) is generated ):Certificatesigningrequest. certsigningrequest

Return to the Apple page and submit the request.

After successful activationAPP IDSProvides an active push certificate for downloading the development or release version.(Aps_development.cer)If you need to perform active server push, you must download this file to use the PUSH Service.

Step 2To provide a certificate for the app to accept the push license, clickProvisioningSet, add a new license, select the newly created app IDs, and then select the iPhone device that can be debugged.

Finally, download the file:Ysypushmessagedemo. mobileprovisionDouble-click the certificate to load it once.

Next, go to the IOS project and select to use this certificate for debugging.

In the red circle, all licenses just loaded are set.

Now, all preparations for the Certificate Class are ready.

Here again, we emphasize the specific role of each file.

1: certificatesigningrequest. certsigningrequest: used to generate app IDS

2: certificates used by aps_development.cer to actively push services

3: ysypushmessagedemo. mobileprovision is a licensed service for apps to receive push notifications.

For more information about the push code and usage, see the first blog.

Here only include: https://github.com/stefanhafeneger/PushMeBaby

Next, let's talk about the code for receiving the push notification.

1: To apply for this app, you must receive push messages from the service provider,

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

2: After the application is sent, if the token is successfully sent, a device token is automatically returned. If the token fails, another failed token is entered.

// Delegate for successful remote notification registration-(void) Application :( uiapplication *) Application didregisterforremotenotificationswithdevicetoken :( nsdata *) devicetoken {nslog (@ "% @", devicetoken); self. viewcontroller. tokenvaluetextview. TEXT = [nsstring stringwithformat: @ "% @", devicetoken]; self. viewcontroller. pushstatuslabel. TEXT = @ "registered. ";}// delegate for failed remote notification registration-(void) Application :( uiapplication *) Application didfailtoregisterforremotenotifswswitherror :( nserror *) error {self. viewcontroller. tokenvaluetextview. TEXT = [error description];}

3: Provide the device token to the service provider for pushing to a specific mobile phone. if a message is pushed remotely, the user clicks the PUSH message, or the application is already on, the system automatically calls the following delegate:

// Click the delegate called when a remote notification is clicked. If the interface is open, the Delegate will directly respond to the-(void) Application :( uiapplication *) Application didreceiveremotenotification :( nsdictionary *) userinfo {nslog (@ "remote notification"); [self pmd_uespushmessage: userinfo];}

4: As described in the third section, the application is in the running state, and the above delegate will be executed. If the application is not enabled, You need to respond to the message again, the following delegate processing is required.

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {// if the application is not started, it is opened through a notification message, now you can get the message. if (launchoptions! = Nil) {nsdictionary * userinfo = [launchoptions objectforkey: uiapplicationlaunchoptionsremotenotificationkey]; [self pmd_uespushmessage: userinfo];} return yes ;}

5: Clear the existing push messages in the notification center. You only need to set the badge of the specified app to 0.

[[UIApplication sharedApplication ] setApplicationIconBadgeNumber:0];

6: The actively pushed strings must conform to the following JSON array format to be correctly pushed to the mobile phone.

@"{

// Custom Parameters

\ "Userinfo \":

{

\ "Name \": \ "remote Notice \"

},

// Standard writing

\ "APs \":

{

\ "Alert \":

{

\ "Action-loc-key \": \ "open \",// Supports multiple languages

\ "Body \": \ "messgae content \"// Message Body

},

\ "Badge \": 1,// Mark the specific value of the app icon

\ "Sound \": \ "Default \"
// Specifies the audio file to be played. The default value indicates the system's default selected ringtone.

}

}";

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.