(96) Enable remote notification, background tasks with APNs

Source: Internet
Author: User
Tags notification center

APNs is all called the apple push Notification Service, enabling the ability to push a server to iOS-specific devices when the app does not start.

APNs implementation principle for the first to send the device's UDID and application bundle identifier to APNs server, APNs encryption into a devicetoken, This devicetoken is the basis for sending push messages to specific UDID devices that must have the appropriate application installed. To send notifications, you only need to send devicetoken and notification content to APNs in a specific format.

To implement remote notifications, you must be a developer, configure a remote debug certificate, and finally get Devicetoken for debugging.

Here's how to use the APNs, starting with the configuration certificate, step-by-step instructions.

"Configuration of remote debugging Certificates"

Log in to member Center, select Certificates,identifiers & Profiles, then click the Certificates folder to enter the certificate configuration.

① first to configure the app Ids,apns prohibit the use of the Blurred app ID, so you must specify an explicit ID for the app to notify.

Click on the app IDs, then click on the right side of the + to enter the certificate creation, fill in the name, Bundle ID (do not use wildcards, to be explicit), pay attention to the last choice of push notifications.

Then continue step by step to complete the creation.

② Configure the Remote debugging certificate, click All in the left certificates, then click + On the right to start creating, note there are two options:

Now the selected item is a debug certificate, the following is the release certificate, debugging only the first configuration, only need to reconfigure a publishing certificate to complete.

Configuring the certificate requires selecting an SCR file, which is from the computer and is created as follows.

Open application, select Keychain Access, and then press Select on the top menu bar:

Fill in the Pop-up dialog box, and note that the Save Mode option saved to disk can be saved to the computer.

Then select this SCR, you can create a remote debugging certificate, download the installation.

Note: When there is a configuration change, you need to regenerate the profiles, click on the provisioning profiles below all, click on the right of the +, step to select the app ID, developer, device, etc., you can generate, download and double-click will jump to Xcode , which means the installation was successful .

The configuration of the certificate is completed by these steps.

"Access to Devicetoken"

Because only the real machine has the UDID, therefore cannot use the simulator to obtain the Devicetoken, the Apple changes the acquisition method after iOS8, but also needs the initiative request authorization, the code is as follows.

if ([[Uidevice currentdevice].systemversion Doublevalue] < 8.0) {    [application Registerforremotenotificationtypes:uiremotenotificationtypebadge];} else{    [Application registerusernotificationsettings:[uiusernotificationsettings settingsForTypes: Uiusernotificationtypebadge Categories:nil]]; Categories the user to reply to the message in the Notification center.    //Unsolicited Request for authorization    [Application registerforremotenotifications];}

There are two methods for receiving Devicetoken, one for receiving tokens and one for receiving error.

Gets the user's current devicetoken when called-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) devicetoken{        NSLog (@ "%@", Devicetoken);    } -(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error {        NSLog (@ "%@", error);    }
If the configuration is not a problem, you can print out the Devicetoken after the device is allowed, and the general usage is to save the token to the server's database to send notifications.

"Send Notification"

To send notifications, more complex, in order to debug can download Pushmebaby, you can simulate the push server to send messages.

Because the project is older, so the compilation will error, just need to put the error of the sentence can be used to watch.

Send notification requires Devicetoken and CER files, CER is just the debug certificate, add it to the project, pay attention to the Add Target, note that the file name should be the same as in the Init method, change to Apns.cer.

Self.certificate = [[NSBundle mainbundle] pathforresource:@ "APNs" oftype:@ "CER"];

Modify the Devicetoken in Init to just print, note that the part in <>, do not include < and >.

To start the software, the interface is as follows:

Click Push to send.

"Receive notifications"

Before the iOS8, is not support the real background, so-called real background refers to the application in the background can also handle UI operations.

The following first describes the notification before iOS8.

①app start, in the background, if you click on Push, a notification will be displayed, if you click this notification, will open the app, and call the following method:

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo{    // UserInfo is the JSON data body that is sent.    }
②app boot, in the foreground, will not be informed, but will still invoke the method above.

③app off, if you click on Push, a notification will be displayed, click the notification will launch the app, will not call the above method, but can be from didfinishlaunchingwithoptions: The value of the Uiapplicationlaunchoptionsremotenotificationkey key of the Launchoptions method is a dictionary, and the JSON data body is stored in it.

"Background Tasks"

Starting with IOS8, you can update the UI in the background, to use this feature, to turn on the switch, and to change the content of the push.

① first follow the open background modes switch, select Remote notifications.

② When a remote notification is received, the following method is called if the app is running:

A remote notification is called and IOS8 uses this to process the background message (non-notification)-(void) Application: (UIApplication *) application didreceiveremotenotification: ( Nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) completionhandler{        Completionhandler (Uibackgroundfetchresultnewdata); The callback must be called to tell the system whether it was successful, so that the system processes the UI when it succeeds.    }
Note that the callback notifies the system whether the process is successful, so that the UI can be updated here, either in the foreground or in the background.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(96) Enable remote notification, background tasks with APNs

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.