Local notifications for iOS
Local notifications are notifications Based on Time behaviors, such as small applications related to calendar or todo lists. In addition, if the app is executed in the background, IOS allows it to run within a limited period of time, and it will also find local notifications useful. For example, an application runs in the background and obtains a message from the application server. When a message arrives, for example, a prompt message for downloading an updated version, a local notification mechanism is used to notify the user.
Local notification is an instance of uilocalnotification, which has three attributes:
- Scheduled time, a time period, used to specify the date and time when the IOS system sends notifications;
- Notification Type, notification type, including warning information, action button title, Badge (digital flag) on the app icon, and playing sound;
- Custom Data. Local notifications can contain local data of the dictionary type.
The maximum number of local notifications allowed by IOS is 64. Local notifications that exceed the limit will be ignored by IOS.
It is very easy to write a simple scheduled reminder, for example:
Uilocalnotification * localnotif = [[uilocalnotification alloc] init];
If (localnotif = nil)
Return;
// Localnotif. firedate = itemdate;
// Nsdate * Now = [nsdate new];
Nsdate * date1 = [nsdate datewithtimeinterval: 8 sincedate: date];
Localnotif. firedate = date1;
Nslog (@ "Now = % @", date1 );
Localnotif. timezone = [nstimezone defaulttimezone];
// Notification Details
Localnotif. alertbody = @ "it's time to eat, notification
........ ";
// Set the action button
Localnotif. alertaction = @ "View ";
Localnotif. soundname = uilocalnotificationdefasoundname;
Localnotif. applicationiconbadgenumber = 1;
// Specify custom data for the notification
Nsdictionary * infodict = [nsdictionary dictionarywithobject: @ "somevalue" forkey: @ "somekey"];
Localnotif. userinfo = infodict;
// Schedule the notification
[[Uiapplication sharedapplication] schedulelocalnotification: localnotif];
[Localnotif release];
// Nsarray * array = [[uiapplication sharedapplication] scheduledlocalconfigurications];
// [[Uiapplication sharedapplication] cancelalllocalconfigurications];
// [[Uiapplication sharedapplication] cancellocalnotification: localnotif];