IOS8 Push the second article

Source: Internet
Author: User

has been updated iOS8, but has not started to study the iOS8, today because the project used to push, so experience the push of iOS8, first talk about this push. Currently divided into four push: User push, local push, remote push, geo-push.

User push

Let's start by talking about this user push, we have to register this push before we use it, the user wants to allow this program to push

registration process:

if  (IS_IOS8)  {          //1. Create a message the action to add (the form of a button is displayed )           uimutableusernotificationaction *action  = [[UIMutableUserNotificationAction alloc] init];           action.identifier = @ "Action";//Button logo            [email protected] "Accept";//the caption of the button            action.activationmode = uiusernotificationactivationmodeforeground;//start the program when clicked            //    action.authenticationrequired  = YES;          //     action.destructive = yes;                     UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction  alloc] init];          action2.identifier = @ "Action2";           [email protected] "Reject";           action2.activationMode =  uiusernotificationactivationmodebackground;//when clicked does not start the program, in the background processing            action.authenticationrequired = yes;//need to be unlocked for processing if action.activationmode =  Uiusernotificationactivationmodeforeground; This property is ignored;           action.destructive = YES;                     //2. Create a Category collection of actions (buttons)            uimutableusernotificationcategory *categorys = [[uimutableusernotificationcategory alloc] init];           categorys.identifier = @ "alert";//the only indication of this set of actions, Push notifications are also based on this to differentiate           [categorys setactions:@[ Action,action2] forcontext: (uiusernotificationactioncontextminimal)];                     //3. Create a uiusernotificationsettings, and set the display class type of the message            Uiusernotificationsettings *notisettings = [uiusernotificationsettings settingsfortypes: ( Uiusernotificationtypebadge | uiusernotificationtypealert | uiremotenotificationtypesound)  categories:[NSSet setWithObjects:categorys, nil nil]];           [application registerusernotificationsettings:notisettings];                 }else{          [application  registerforremotenotificationtypes: uiremotenotificationtypealert |  uiremotenotificationtypebadge | uiremotenotificationtypesound];      }      -  (void) application: (uiapplication *) application  Didregisterusernotificationsettings: (uiusernotificationsettings *) notificationsettings  {   //    UIUserNotificationSettings *settings = [application  currentusernotificationsettings];  //    uiusernotificationtype types  = [settings types];  //    //only 5 and 7 included   uiusernotificationtypebadge  //    if  (types ==  5 | |  TYPES == 7)  {  //         application.applicationiconbadgenumber = 0;  //    }       //Registering remote Notification       [application registerforremotenotifications ];  }


We are now only registered notification settings, but also to register the behavior of Push notification, in iOS8, the behavior can be directly in the push message, such as reply message, reject Message summary is three methods to register


How we can do this, first of all we need to register these behaviors.

  • Actions

  • Uimutableusernotificationaction *acceptaction = [[Uimutableusernotificationaction alloc] init];  Acceptaction.identifier = @ "Rickaction";  Acceptaction.title = @ "Accept";  Acceptaction.activationmode = Uiusernotificationactivationmodebackground;  Acceptaction.destructive = NO; acceptaction.authenticationrequired = NO;
  • Categories

  • Uimutableusernotificationcategory *invitecategory = [[Uimutableusernotificationcategory alloc] init];  Invitecategory.identifier = @ "Invite_category"; [Invitecategory setactions:@[acceptaction] forcontext:uiusernotificationactioncontextdefault];

  • We need to pay attention to this uiusernotificationactioncontextdefault, if we use this, we will get this push behavior, maybe and accept

    We can also use Uiusernotificationactioncontextminimal to get decline and accept behavior

  • Settings

    After these actions are registered, we have completed the process of registering the push with the previously mentioned push settings.

Nsset *categories = [Nsset setwithobjects:invitecategory, nil Nil]; Uiusernotificationtype types = Uiusernotificationtypebadge | Uiusernotificationtypesound |  Uiusernotificationtypealert;  Uiusernotificationsettings *mysettings = [uiusernotificationsettings settingsfortypes:types categories:categories]; [[UIApplication sharedapplication] registerusernotificationsettings:mysettings];
Remote push

Remote push, all message size not more than 2KB, we get the remote push JSON format message, parsing this message is our remote push:

{"APS": {"content-available": 1, "alert": "This is the alert text", "badge": 1, "Sound": "Default"}

To use a remote push, two conditions are met: First, the user needs to call the registered user to push registerusernotificationsettings; second, the uibackgroundmodes must contain remote notifications in the Info.plist file.

[[uiapplication sharedapplication]  registerforremotenotifications];-  (void) application: (uiapplication *) application  Didregisterforremotenotificationswithdevicetoken: (nsdata *) devicetoken {       nsstring *token=[nsstring stringwithformat:@ "%@",devicetoken];       token=[token stringbyreplacingoccurrencesofstring:@ "<"  withstring:@ ""];       token=[token stringbyreplacingoccurrencesofstring:@ ">"  withString:@ ""];       token=[token stringbyreplacingoccurrencesofstring:@ " "   withstring:@ ""];  }-  (void) application: (uiapplication *) application  Didfailtoregisterforremotenotificationswitherror: (nserror *) error {        } 

IOS7 Notification Agent Method

Later, the proxy method for local notifications was added.

IOS8 's push agent method is only two.

-  (void) application: (uiapplication *) Application handleactionwithidentifier: (NSString *) Identifier forlocalnotification: (uilocalnotification *) Notification completionhandler: (void   (^) ()) completionhandler  {  }    -  (void) Application: ( uiapplication *) Application handleactionwithidentifier: (nsstring *) identifier  Forremotenotification: (nsdictionary *) Userinfo completionhandler: (void  (^) ()) CompletionHandler   {  }    -  (void) application: (uiapplication *) application  handleactionwithidentifier: (nsstring *) Identifier forlocalnotification: (UILocalNotification  *) Notification completionhandler: (void  (^) ()) completionhandler  {       if  ([identifier isequaltostring:@ "Rickaction"])  {           [self handleacceptactionwithnotification:notification];      }       completionhandler ();  }    -  (void) Handleacceptactionwithnotification: (uilocalnotification*) notification  {  }
geo-Location push

This push is a feature of the new API and must be used in conjunction with cllocation positioning.

location notification      cllocationmanager *locman = [[ cllocationmanager alloc] init];      locman.delegate = self;       [locman requestwheninuseauthorization];    # pragma mark - cllocationmanager    -  (void) LocationManager: ( cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status     {      BOOL canUseLocationNotifications =  (Status  == kclauthorizationstatusauthorizedwheninuse);      if  ( Canuselocationnotifications)  {          [self  startshowlocationnotification];      }  }  -  (void) Application: (uiapplication *) APPLICATION&NBSp;didreceivelocalnotification: (uilocalnotification *) notification    {       CLRegion *region = notification.region;       if  (region)  {      }  }    -  (void) startshowlocationnotification    {       cllocationcoordinate2d local2d ;      local2d.latitude =  123.0;      local2d.longitude = 223.0;       UILocalNotification *locNotification = [[UILocalNotification alloc] init];       locnotification.alertbody = @, "You Got it,";    .   locNotification.regionTriggersOnce = YES;       Locnotification.region = [[clcircularregion alloc] initwithcenter:local2d radius:45 identifier:@ "Local-identity"];       [[UIApplication sharedApplication] scheduleLocalNotification:locNotification];   }

If the core location is not opened then the above didreceivelocalnotification will not be called

Finally, the whole push process I think this is the case, first register push, and then push the message, the client receives push message, perform push behavior. If there is an error, please also comment below the article, welcome correction.



IOS8 Push the second 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.