In the current order of the application of the message processing needs more and more, the system needs to be a message in the form of audio or text prompts the user, here is used to push, push message mainly local and remote push, today we first study a simple local notice, the following alarm clock as an example.
1, we first want to register notice
UIApplication * application=[UIApplication sharedapplication]; //If the current application does not register local notifications, you need to register if([Application currentusernotificationsettings].types==Uiusernotificationtypenone) { //How to set prompt support//uiusernotificationtypebadge hint icon//uiusernotificationtypesound hint Sound//uiusernotificationtypealert prompt Bullet boxUiusernotificationsettings * Setting=[uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge| uiusernotificationtypesound|Uiusernotificationtypealert Categories:nil]; [Application registerusernotificationsettings:setting]; } //Delete a previous duplicate notification[Application cancelalllocalnotifications]; Notice that after registration is complete, you can view it in the settings and delete it.
2. Set up Notifications
#pragmaMark-Add local notification-(void) _addlocalnotification: (NSDate *) date{uilocalnotification* noti=[[Uilocalnotification alloc] init]; //Set start timeNoti.firedate=date; //Set Bodynoti.alertbody=@"It's time to get up"; //Set Actionnoti.alertaction=@"Unlock"; //Setting AlarmsNoti.soundname=@"4195.mp3"; #warningIf you do not delete after registration, the next time will continue to exist, even if you uninstall from the emulator will be retained//Registration Notice[[UIApplication sharedapplication] schedulelocalnotification:noti]; }
This will cause the alarm to sound in the set time,
3, so the function of the alarm clock is basically implemented, but there is a problem, because if the current program is open will cause the alarm will not sound, then how do we solve the problem. At this point we need to use the player to solve
@interfaceappdelegate () {//define the player to play musicAvaudioplayer *player; //to determine whether to open from the notification windowBOOL isfromnotification;}- (void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) notification{//If you are coming in from the notification window, you do not need to play audio if(isfromnotification) {return; } //Initialize music to play musicNsurl * Url=[[nsbundle Mainbundle] Urlforresource:@"4195.mp3"Withextension:nil]; Player=[[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil]; Player.numberofloops=0; [Player Preparetoplay]; [Player play]; }
That's it.
To learn more about the small partners, you can click to view the source code , run the test yourself.
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Alarm for local notifications