Detailed IOS local push and remote push-send _ios

Source: Internet
Author: User

First, Introduction

It is divided into 2 kinds of local push and remote push. The user can be prompted if the application is not turned on or even the phone is locked. They all need to register, after registering the system will pop up the prompt box (below) prompts the user to agree, if the consent is normal use; If the user does not agree, the next open program will not pop-up the prompt box, you need to set the user settings. There are three types of prompts:

Uiusernotificationtypebadge: information tip in the upper right corner of the application icon

Uiusernotificationtypesound: play beep

Uiusernotificationtypealert: Prompt box

Second, local push

1 Registration and processing

The code is as follows:

 Generally at the start of the registration notice, the program was killed, click on the notification call after the program
-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions {
 if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0) {//iOS8
 uiusernotificationsettings *setting = [Uiusernotificationsettings settingsForTypes : Uiusernotificationtypebadge | Uiusernotificationtypealert | Uiusernotificationtypesound Categories:nil];
 [Application registerusernotificationsettings:setting];
 }

 if (Launchoptions[uiapplicationlaunchoptionslocalnotificationkey]) {
 ///Add processing code here
 } return
 YES;
}
The program is not killed (in the foreground or backstage), the program is called after clicking the notification
-(void) Application: (UIApplication *) Application Didreceivelocalnotification :(uilocalnotification *) Notification {
 //Add processing code here
}

As you can see, there are two ways to handle code, one is

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification;
Another is
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions;

If the program is not killed, that is, in the foreground or backstage, then call the former; if the program is killed, call the latter.

2 Send notification

The code is as follows

-(Ibaction) addlocalnotification {
 //1. Create a local notification
 uilocalnotification *localnote = [[Uilocalnotification alloc ] init];

 1.1. Set the time of notification
 localnote.firedate = [NSDate datewithtimeintervalsincenow:5];

 1.2. Set the notification content
 Localnote.alertbody = @ "This is a push this is a push";

 1.3. When setting the lock screen, a text displayed below the font
 localnote.alertaction = @ "hurriedly!!!!!";
 Localnote.hasaction = YES;

 1.4. Set the startup picture (opened by notification)
 localnote.alertlaunchimage = @ ". /documents/img_0024.jpg ";

 1.5. Set through the incoming sound
 localnote.soundname = uilocalnotificationdefaultsoundname;

 1.6. Set the application icon to display the upper left corner of the number
 localnote.applicationiconbadgenumber = 999;

 1.7. Set up some additional information
 localnote.userinfo = @{@ "QQ": @ "704711253", @ "MSG": @ "Success"};

 2. Notification of implementation
 [[UIApplication sharedapplication] schedulelocalnotification:localnote];
}

The effect is as follows:

3 Cancellation Notice

Cancel all local alerts
[Application cancelalllocalnotifications];

Third, remote push

Unlike Android's own implementation of the push service, Apple has a very strict control over the device, and the message push process has to go through APNs (Apple push Notification service).

In general, if a program is back to the background can not run the code (Audio, VOIP, etc. in the background to run), or after the program quit, then it and the corresponding application of the backend server disconnected the link, will not receive the information sent by the server, But each device as long as the network will be connected with Apple's APNs server to establish a long connection (persistent IP connection), so long as the APNs server through the Apple, our own server can be indirectly connected with the device, the schematic is as follows:

Use steps:

1 tick backgroud Modes-> remote notifications, mainly after the iOS7, Apple support background operation, if opened here, when received remote push, the program in the background can also do some processing, as shown in the following figure:

2 Remote push registration and local push is different, iOS8.0 before and after the different, code see below.

In addition, the first time you use a push, there may be a question: Didfinishlaunchingwithoptions will be invoked every time the program is opened, is not every time the registration function will be called, each time the window will be asked the user "whether to allow push notifications"? In fact, this window will only pop up once when the program is first opened, regardless of whether the user allows or not allow Apple to remember the user's choice, the registration function calls many times to the user has no effect

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
 IOS8 should be treated differently after and before
 if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0) {
 Uiusernotificationsettings *settings = [Uiusernotificationsettings Settingsfortypes:uiusernotificationtypebadge | Uiusernotificationtypealert | Uiusernotificationtypesound Categories:nil];
 [[UIApplication sharedapplication] registerusernotificationsettings:settings];
 } else {
 [Application Registerforremotenotificationtypes:uiremotenotificationtypealert | Uiremotenotificationtypebadge | Uiusernotificationtypesound];
 }

 Return YES
}
The meaning of this function is that when the user closes the notification in the setting, the function is invoked when the program starts, and we can get the user's settings
-(void) Application: (UIApplication *) application Didregisterusernotificationsettings: (uiusernotificationsettings *) notificationsettings {
 [application Registerforremotenotifications];
}

3 If the registration fails, such as the absence of a certificate, etc., it will call:

Registration failure call
-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: ( Nserror *) Error {
 NSLog (@ "Remote notification registration failed:%@", error);
}

4 Get Devicetoken

If the user agrees, Apple will generate Devicetoken based on the application's Bundleid and cell phone udid, and then call application Didregister method to return Devicetoken, the program should send Devicetoken to the application service , the server is obligated to store it (multiple Devicetoken may be saved if multiple logons are allowed). Devicetoken will also change: "If the user restores backup data to a new device or computer, or reinstalls the operating system, the D Evice token changes "should be sent to the server every time (provider)

When the user agrees, the program is invoked to obtain the devicetoken of the system, and the Devicetoken should be passed to the server for saving, which is invoked each time the program starts (provided the user allows notification)
-(void) Application: ( UIApplication *) Application Didregisterforremotenotificationswithdevicetoken: (NSData *) DeviceToken {
 NSLog (@) Devicetoken =%@ ", devicetoken);
}

5 users clicked on the notification

The program is opened by default. The processing code has three functions, before iOS7 and whether the program is in the background

5.1 IOS7 and the following

This function whether the program is killed or in the background, as long as the user clicks on the notification, will be called, so if it is iOS7, you do not have to do in the didfinishlaunchingwithoptions processing, only in the following function to do processing, You should avoid duplicate processing in the Didfinishlaunchingwithoptions function at this point.

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler {
 //UserInfo
} 

Note: When you open the background operation in the first step, the user does not click on the notification, you can also execute:

 

Before 5.2 iOS7

When the user clicks on the notification, if the program is killed it will call the following first function, if the program is in the background will call the following second function, so the following two functions should be used in combination with

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
 Get remote Push message
 nsdictionary *userinfo = Launchoptions[uiapplicationlaunchoptionsremotenotificationkey];
 if (userInfo) {
 //There is a push message, processing the push message
 } return
 YES;
}
IOS3 only after the program is in the background, the user clicks on the notification will be invoked, should be paired with Didfinishlaunchingwithoptions use
-(void) Application: (UIApplication * ) Application didreceiveremotenotification: (Nsdictionary *) UserInfo {
 //UserInfo
}

In actual programming, if you want to be compatible with IOS7 before, three functions can be used at the same time, are listed, the system will automatically select the appropriate call.

6 Summary of the following function calls:

Start after first installation:

Didregisterforremotenotificationswithdevicetoken is called

The system asks the user if they agree to receive notifications

Didregisterusernotificationsettings is invoked regardless of the user's choice of consent or rejection

When the application is not first started:

If notifications is in a deny state: Didregisterusernotificationsettings is called

If notifications is in the allowed state

Didregisterforremotenotificationswithdevicetoken is called

Didregisterusernotificationsettings is called

User modifies notifications settings during application run:

Change from deny to allow: Didregisterforremotenotificationswithdevicetoken is called

Change from allow to deny: nothing happens

7 service-Side push format

{
 "APS": {   //must have
 "alert": "string",
 "Body": "string",
 "badge": number,
 "sound": "String" 
   },
 "Notiid": 20150821,//Custom key value
}

8 size limit for push

The size of the remote notification payload varies according to the APIs used by the server. When the HTTP/2 provider API is used, the maximum load is 4kB, and the maximum load is 2kB when legacy binary interface is used. APNs will refuse to send this notification when the load size exceeds the specified load size.

9 as shown in the following figure (in the case of micro-mail push):

10 Finally, there is a need to apply for a certificate, which no longer details-_-

This article has been organized into the "iOS push Tutorial", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.