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];} }
iOS local push