Local push for iOS development

Source: Internet
Author: User

Web push may be the most important, but local push can sometimes be used in projects;

The code below is a little gossip:

1. Add Root View

Self.window.rootViewController = [[Uinavigationcontroller Alloc]initwithrootviewcontroller:[[viewcontroller Alloc]init]];         = [Uicolor whitecolor];    [Self.window makekeyandvisible];

2. Create a button locally to trigger

button = [UIButton buttonwithtype:uibuttontypecustom];     = CGRectMake (+ +);     = [Uicolor bluecolor];    [Button Settitle: @" Here we go. " Forstate:uicontrolstatenormal];    [Button Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];     // Binding Method     [button addtarget:self action: @selector (Noticclick:) forcontrolevents:uicontroleventtouchupinside];    [Self.view Addsubview:button];

3, register a notice

//set local notifications pass a time in+ (void) Registerlocalnotification: (nsinteger) alerttime{uilocalnotification*notification =[[Uilocalnotification alloc]init]; //set the time to trigger notificationsNSDate *firedate =[NSDate Datewithtimeintervalsincenow:alerttime]; NSLog (@"firedate=%@", firedate); Notification.firedate=firedate; //Time ZoneNotification.timezone =[Nstimezone Defaulttimezone]; //set the recurrence intervalNotification.repeatinterval =Kcfcalendarunitsecond; //Notification ContentNotification.alertbody =@"It's time to get up ..."; Notification.applicationiconbadgenumber=1; //sounds that are played when notifications are triggeredNotification.soundname =Uilocalnotificationdefaultsoundname; //Notification ParametersNsdictionary *userdict = [Nsdictionary dictionarywithobject:@"Got up and started learning about iOS developed"Forkey:@"Key"]; Notification.userinfo=userdict; //after iOS8, you need to add this registration to be authorized    if([[UIApplication sharedapplication] Respondstoselector: @selector (registerusernotificationsettings:)]) {UIUser NotificationType type= Uiusernotificationtypealert | Uiusernotificationtypebadge |Uiusernotificationtypesound; Uiusernotificationsettings*settings =[Uiusernotificationsettings Settingsfortypes:type        Categories:nil];        [[UIApplication sharedapplication] registerusernotificationsettings:settings]; //the unit that notifies the repeating prompt, which can be day, week, monthNotification.repeatinterval =Nscalendarunitday; } Else {        //the unit that notifies the repeating prompt, which can be day, week, monthNotification.repeatinterval =Nsdaycalendarunit; }    //Perform notification registration[[UIApplication sharedapplication]schedulelocalnotification:notification]; }

4. Call this method

-(void) Noticclick: (ID) sender{    // Call notification    [Viewcontroller Registerlocalnotification:4]; // 4 seconds later }

5, the method of cancellation notice

//cancel a local push notification+(void) Cancellocalnotificationwithkey: (NSString *) key{//get all local notification arraysNsarray *localnotifications =[UIApplication sharedapplication].scheduledlocalnotifications;  for(Uilocalnotification *notificationinchlocalnotifications) {Nsdictionary*userinfo =Notification.userinfo; if(userInfo) {//get notification parameters based on the key specified when setting the notification parametersNSString *info =Userinfo[key]; //If you find a notification that you want to cancel, cancel            if(Info! =Nil)                {[[UIApplication sharedapplication]cancellocalnotification:notification];  Break; }                    }    }}

6. Call this method

//Local notification callback function that is called when the application is in the foreground-(void) Application: (UIApplication *) application didreceivelocalnotification: (nonnull uilocalnotification *) notification{NSLog (@"notif:%@", notification); //It's really a place to deal with interactions.//get the data with the notificationNSString *notmes = [Notification.userinfo objectforkey:@"Key"]; Uialertview*alertview = [[Uialertview alloc]initwithtitle:@"Local notification (front desk)"Message:notmesDelegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil];        [Alertview show]; //update the number of corner labels displayedNsinteger badge =[UIApplication Sharedapplication].applicationiconbadgenumber; Badge--; Badge= Badge >=0? Badge:0; [UIApplication Sharedapplication].applicationiconbadgenumber=badge; //You can cancel the push when you don't want to push it again.[Viewcontroller Cancellocalnotificationwithkey:@"Key"];}

Local push for iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.