iOS local push

Source: Internet
Author: User

Timed push via iOS local push

  • Request a push license in Appdelegate's Didfinishlaunch method first
    if(@available (IOS10.0, *)) {
    Unusernotificationcenter*center =[Unusernotificationcenter Currentnotificationcenter]; [Center requestauthorizationwithoptions: (Unauthorizationoptionalert+unauthorizationoptionsound) completionhandler:^ (BOOL Granted, Nserror *_nullable Error) { }]; Center.Delegate=Self ; } Else { //Fallback on earlier versions[Application registerusernotificationsettings:[uiusernotificationsettings Settingsfortypes: uiusernotificationtypesound| Uiusernotificationtypealert Categories:[nssetSet]] ]; }

  • Receive local notifications for processing
IOS10 before receiving local notifications
-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification{ NSLog (@ "Receive local notifi%@", Notification.userinfo);}
 
NSLog (@ " Willpresentnotification local notifi%@ ", notification.request.content.userInfo);
//delegate to Didreceivenotificationresponse processing

}
-(void) Usernotificationcenter: (unusernotificationcenter *) Center Didreceivenotificationresponse: ( Unnotificationresponse *) Response Withcompletionhandler: (void (^) (void)) Completionhandler Ns_available_ios (10) { NSLog (@ "Usernotificationcenter local notifi%@", response.notification.request.content.userInfo); if ([Response.actionidentifier isequaltostring:@ "sure"]) { NSLog (@ "notif sure"); } else if ([Response.actionidentifier isequaltostring:@ "Cancel"]) { NSLog (@ "Notif cancel"); } Completionhandler ();}

Set up local push

-(void) Addalarmindentifier: (nsstring*) requestindetifier isrepeat: (BOOL) repeat{nsdatecomponents*cmp = [[NSCalendar Currentcalendar] Components:nscalendarunithour| nscalendarunitminute|    Nscalendarunitweekday FromDate:self.datepicker.date];    Nsdatecomponents *components = [[Nsdatecomponents alloc] init];    Components.hour = Cmp.hour;    Components.minute = Cmp.minute;    Components.weekday = Cmp.weekday;    Notification sound NSString *soundname = @ "Soundname"; if (@available (IOS 10.0, *)) {Unusernotificationcenter *center = [Unusernotificationcenter currentnotificationcent        ER];        Unmutablenotificationcontent *content = [[Unmutablenotificationcontent alloc] init];        Content.title = @ "Wake up!!";        Content.body = @ "time to massage";        category, for customizing notification actions Content.categoryidentifier = @ "Alarm";             Content.userinfo [email protected]{@ "name": @ "Jack"};        Notification sound content.sound = [Unnotificationsound soundnamed:soundname]; Trigger RepeATS: Represents whether to repeat uncalendarnotificationtrigger *trig = [Uncalendarnotificationtrigger triggerwithdatematchingcomponents:c Omponents repeats: (Repeat?        Yes:no)]; Unnotificationrequest *request = [Unnotificationrequest requestwithidentifier:requestindetifier content:content        Trigger:trig]; Action 1 unnotificationaction*action1 = [unnotificationaction actionwithidentifier:@ "Sure" title:@ "sure" options:UNNot        Ificationactionoptionforeground]; Action 2 Unnotificationaction*action2 = [unnotificationaction actionwithidentifier:@ "Cacel" title:@ "Cancel" Options:UN        Notificationactionoptionauthenticationrequired]; Identifier:alarm unnotificationcategory*category = [unnotificationcategory categorywithidentifier:@ "Alarm" act        Ions:@[action1,action2] intentidentifiers:@[] options:unnotificationcategoryoptioncustomdismissaction];        Set up classification [center setnotificationcategories: [Nsset setwithobject:category]; Set up notifications [center AddnotificationrEquest:request withcompletionhandler:^ (Nserror * _nullable error) {if (error) {NSLog (@ "error==            =%@ ", error.localizeddescription);                    }        }];        } else {//ios 10 below nsdateformatter *dateformatter = [[NSDateFormatter alloc] init];        Dateformatter.dateformat = @ "Yyyy-mm-dd hh:mm";        Uilocalnotification *notification = [[Uilocalnotification alloc] init];                If a loop can be used in the past time as standard, then set repeatinterval notification.firedate = Self.datepicker.date;        Notification.repeatcalendar = [Nscalendar Currentcalendar]; Notification.repeatinterval = repeat?        nscalendarunitweekofyear:0;        Nsminutecalendarunit Notification.timezone = [Nstimezone systemtimezone];        Notification.alertbody = @ "Wake up!!!";        Notification.soundname = Soundname; [[UIApplication Sharedapplication] ScheduleloCalnotification:notification]; }    }

Cancel notification (cancel all)

-(void) cancel{     if (@available (IOS 10.0, *)) {        [[Unusernotificationcenter Currentnotificationcenter] Removealldeliverednotifications];    } else {[          [uiapplication sharedapplication] cancelalllocalnotifications];}    }
    • Effects such as:

iOS local 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.