objective c - iOS and local Notifications -
is there way send local notification app app?
i need send notification app users every morning. so, can add code app, after user launch that, every morning or badge/notification?
you can add local notifications ios app doing following:
step one
register local notifications in app delegate:
-(bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // register app local notifcations. if ([application respondstoselector:@selector(registerusernotificationsettings:)]) { [application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert | uiusernotificationtypebadge | uiusernotificationtypesound categories:nil]]; } // setup local notification check. uilocalnotification *notification = [launchoptions objectforkey:uiapplicationlaunchoptionslocalnotificationkey]; // check if notifcation has been received. if (notification) { dispatch_async(dispatch_get_main_queue(), ^{ // run notifcation. // call own custom method here. // use [notification.userinfo valueforkey:@"notification_id"] associated notification id (you need assign id, when creating notification). }); } // ensure notifcation badge number hidden. application.applicationiconbadgenumber = 0; return yes; }
step two
use following method create local notification:
-(void)savenotification:(nsstring *)description :(nsstring *)notificationid :(bool)locationcheck { // create notification info dictionary // , set notification id string. nsmutabledictionary *userinfo = [[nsmutabledictionary alloc] init]; [userinfo setobject:notificationid forkey:@"notification_id"]; // setup local notification. uilocalnotification *localnotification = [[uilocalnotification alloc] init]; // set notification id , type data. localnotification.userinfo = userinfo; // set notification description. localnotification.alertbody = [nsstring stringwithformat:@"%@", description]; // set sound alert mp3 file. localnotification.soundname = [nsstring stringwithformat:@"notification_sound_file.mp3"]; // set date notification or set // location depending on notification type. if (locationcheck == no) { // fire date of choice. nsdate *yourfiredate; // set reminder date. double interval = [yourfiredate timeintervalsincenow]; localnotification.firedate = [[nsdate date] datebyaddingtimeinterval:interval]; localnotification.timezone = [nstimezone systemtimezone]; // set notifcation repeat interval. localnotification.repeatinterval = 0; // no repeat. //localnotification.repeatinterval = nscalendarunithour; // every hour. //localnotification.repeatinterval = nscalendarunitday; // every day. //localnotification.repeatinterval = nscalendarunitweekofyear; // once week. //localnotification.repeatinterval = nscalendarunitmonth; // once month. //localnotification.repeatinterval = nscalendarunityear; // once year. } else if (locationcheck == yes) { // set locaton selected address co-ordinates. cllocationcoordinate2d coordinates = cllocationcoordinate2dmake(latitude, longitude); clcircularregion *region = [[clcircularregion alloc] initwithcenter:coordinates radius:100 identifier:[nsstring stringwithformat:@"region_%@", notificationid]]; // set notification presented // when user arrives @ location. [region setnotifyonentry:yes]; [region setnotifyonexit:no]; // set notification location data. [localnotification setregion:region]; [localnotification setregiontriggersonce:no]; } // save local notification. [[uiapplication sharedapplication] schedulelocalnotification:localnotification]; }
you need create own unique id, in order use method. id important, because distinguish between notifications (should need perform specific action depending on notification).
you can call above method so:
[self savenotification:@"test notification hello world" :@"unique id" :no];
don't forget replace latitude
, longitude
descried co-ordianted (if need location based local notifications).
step three
if app open (in foreground or via multitasking), need implement method in app delegate, in order handle notifications:
-(void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification { // check if notifcation has been received. if (application.applicationstate == uiapplicationstateinactive) { // app in running in background active, // should user tap ios notification banner. // call own custom method here. // use [notification.userinfo valueforkey:@"notification_id"] associated notification id (you need assign id, when creating notification). } else if (application.applicationstate == uiapplicationstateactive) { // app open in foreground // need display alert or banner // in app alert user. // call own custom method here. // use [notification.userinfo valueforkey:@"notification_id"] associated notification id (you need assign id, when creating notification). } // ensure notifcation badge number hidden. application.applicationiconbadgenumber = 0; }
a few other points keep in mind
- you can set maximum of 64 local notifications. (notifications repeat counted 1 local notification). https://developer.apple.com/library/ios/documentation/iphone/reference/uilocalnotification_class/
- a local notification cannot have firedate , location region. if want same notification appear @ given time , location, have create 2 separate local notifications same description (one date , other location).
you can use following methods delete local notifications (or specific one):
-(void)deleteallnotifications { // delete local notifications. [[uiapplication sharedapplication] setapplicationiconbadgenumber:0]; [[uiapplication sharedapplication] cancelalllocalnotifications]; } -(void)deletespecificnotification:(nsstring *)inputid { // notification(s) data. nsarray *notificationdata = [[uiapplication sharedapplication] scheduledlocalnotifications]; // loop through local notifcations , delete // notifications match input id string. (int loop = 0; loop < [notificationdata count]; loop++) { // notification object. uilocalnotification *localnotification = [notificationdata objectatindex:loop]; // if notification id matches input id delete it. if ([[localnotification.userinfo objectforkey:@"notification_id"] isequaltostring:inputid]) { [[uiapplication sharedapplication] cancellocalnotification: localnotification]; } } }
Comments
Post a Comment