When you need to send a local push, we need to set the firetime to send time, a lot of sample code on the Web is simply to set a time similar to 10 seconds later, but we may need to be in a custom or user defined a specific time to send, in fact, it is not difficult, Is the knowledge point of OC--the use of the time class of the common class.
First we need a specific time date, and we'll split it according to that time. This time is usually from the user set time.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd hh:mm";
NSDate *testdate = [formatter dateFromString:model.testTime];
Nsassert (testdate!= Nil, @ "testdate = nil");
Second, splitting time requires two very important classes: the Nscalender class and the Nsdatecomponent class. Initializes the two variables, specifying units for the Nsdatecomponent class.
To set the fire date
Nscalendar *calender = [Nscalendar Autoupdatingcurrentcalendar];
Nsdatecomponents *datecomponents = [Calender Components:nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday Fromdate:testdate];
With the Component object, we are free to set the time for it. For example, I would like to remind seven points, for the component hour object Assignment 7 can be. You can then get a Date object from component.
[Datecomponents Sethour:7];
NSDate *firedate = [Calender datefromcomponents:datecomponents];
NSLog (@ "Fire date =%@", firedate);
This enables the ability to set a specific time. The next part is the knowledge of local notifications.
Declare a Uilocalnotification object (pay attention to error judgment), set various properties for it, and invoke the application agent to send out the notification.
Uilocalnotification *noti = [[Uilocalnotification alloc] init];
if (Noti = = nil) {return
;
}
[Noti Settimezone:[nstimezone Defaulttimezone]];
Noti.firedate = firedate;
Noti.repeatinterval = 0;
Noti.alertbody = [NSString stringwithformat:@] The%@ exam is about to begin today at%d, where is%@, are you ready? ", Model.testname, Datecomponents.hour, model.testlocation];
Noti.alertaction = @ "View";
Noti.soundname = Uilocalnotificationdefaultsoundname;
Noti.applicationiconbadgenumber + 1;
Noti.userinfo = @{@ "Kind": @ "Testbeginlocalnotification"};
[[UIApplication sharedapplication] schedulelocalnotification:noti];
In the end, don't forget to launch in the Appdelegate.
-----Appdelegate's Didfinishlaunchingwithoptions method-------
//reset the application icon badge
number Application.applicationiconbadgenumber = 0;
Handle launching from a notification
uilocalnotification *localnotif =
[launchoptions objectforkey: Uiapplicationlaunchoptionslocalnotificationkey];
if (localnotif) {
NSLog (@ "recieved Notification%@", localnotif);
}
Attach the complete code in your application for reference error correction--
-(void) Setuplocalnotificationwithmodel: (Testmodel *) model {if (Model.shouldremind = NO) {return;
} uilocalnotification *noti = [[Uilocalnotification alloc] init];
if (Noti = = nil) {return;
} [Noti Settimezone:[nstimezone Defaulttimezone]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd hh:mm";
NSDate *testdate = [Formatter dateFromString:model.testTime];
Nsassert (testdate!= Nil, @ "testdate = nil");
To set the fire date nscalendar *calender = [Nscalendar Autoupdatingcurrentcalendar]; Nsdatecomponents *datecomponents = [Calender Components:nscalendarunityear | Nscalendarunitmonth |
Nscalendarunitday Fromdate:testdate];
[Datecomponents Sethour:7];
NSDate *firedate = [Calender datefromcomponents:datecomponents];
NSLog (@ "Fire date =%@", firedate);
Noti.firedate = firedate;
Noti.repeatinterval = 0; to GET the hour datecomponents = [calender components:nscalendarunithour fromdate:testdate];
NSLog (@ "Test date hour =%d", datecomponents.hour); Noti.alertbody = [NSString stringwithformat:@] The%@ exam is about to begin today at%d, where is%@, are you ready?
", Model.testname, Datecomponents.hour, model.testlocation];
NSLog (@ "Tip:%@", noti.alertbody);
Noti.alertaction = @ "View";
Noti.soundname = Uilocalnotificationdefaultsoundname;
NSLog (@ "Notification application icon Badge number =%d", noti.applicationiconbadgenumber);
Noti.applicationiconbadgenumber + 1;
Noti.userinfo = @{@ "Kind": @ "Testbeginlocalnotification"};
[[UIApplication sharedapplication] schedulelocalnotification:noti]; }