iOS local push and remote push

Source: Internet
Author: User

Divided into local push and remote push 2 kinds. You can give the user a hint when the app is not open or even the phone lock screen. They all need to register, after registering the system will pop up a prompt box (such as) prompt the user whether consent, if the consent is normal use; If the user does not agree to the next time the program will not pop up the prompt box, you need to set the user to the settings. A total of three types of tips:

Uiusernotificationtypebadge//Application icon in the upper right corner of the information tip

Uiusernotificationtypesound//Play Beep

Uiusernotificationtypealert//Cue Box

  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 this 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 in the background), after clicking on the notification will call this program-(void) Application: (UIApplication *) application didreceivelocalnotification: ( Uilocalnotification *) Notification {//Add processing code here}     

As you can see, there are two places to process code, and one is

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification, The other is
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions If the program is not killed, either in the foreground or in the background, the former is called, and if the program is killed, the latter is called.
2 Sending Notifications
 
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 which is a push";        1.3. When setting the lock screen, a text displayed below the font    localnote.alertaction = @ "HURRY!!!!!" ;    Localnote.hasaction = YES;//1.4. Set the boot picture (opened by notification) Localnote.alertlaunchimage = @ "... /documents/img_0024.jpg ";//1.5. Set the sound by arrival localnote.soundname = uilocalnotificationdefaultsoundname; 1.6. Set the application icon to display the upper left corner of the number localnote.applicationiconbadgenumber = 999;//1.7. Set some additional information Localnote.userinfo = @{@ "QQ": @ " 704711253 ", @" MSG ": @" Success "}; 2. Execution notice [[uiapplication sharedapplication] schedulelocalnotification:localnote];}     

The effect is as follows:

  Remote push

  Unlike the push service we implement on Android, Apple controls the device very strictly, and the process of message push must go through APNs (for example):

Here Provider refers to the developer of an application.

1 Remote push registration is different from local push, iOS8.0 is different before and after, the specific code is as follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary * ) launchoptions {if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0 ) {Uiusernotificationset Tings *settings = [uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge|         Uiusernotificationtypealert Categories:nil];        [[UIApplication sharedapplication] registerusernotificationsettings:settings];    You also need to call [application Registerforremotenotifications] in the didregisterusernotificationsettings callback; } else  {[Application Registerforremotenotificationtypes:uiremotenotificationtypealert |     Uiremotenotificationtypebadge]; } return  YES;} -(void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings * ) notificationsettings {[Application registerforremotenotifications];}       

2 If registration fails, such as no certificate, etc., it will be called:

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

3 If the user agrees, Apple will generate Devicetoken based on the app's Bundleid and phone udid, and then call application's Didregister method to return to Devicetoken, the program should send Devicetoken to the app's Server, the server is obligated to store it (multiple devicetoken may be stored if multiple-point logons are allowed). The preparation will be finished.

After the user agrees, this program is called to get the system's devicetoken-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {    NSLog (@ "Devicetoken =%@", Devicetoken);} 

4 The user clicks the notification and opens the program by default. There are two places to process code, one is

-(void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo; the other is

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions; If the program is not killed, either in the foreground or in the background, the former is called, and if the program is killed, the latter is called.

5 Overall as shown:

 

 In addition, Devicetoken can be recognized as a device and more durable than IDFA because it is the bundleid of the application and the udid of the cell phone. As follows:

Remove an app from a new installation: unchanged

Factory Reset: Unchanged

After deleting the program, upgrade the system and install: unchanged

Reload System (Recovery): Unchanged

Reload system (reactivate, not set to new iphone, use Backup): Unchanged

Reload system (reactivate, set as new iphone without backup): change, rarely appearing

iOS local push and remote 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.