iOS local push

Source: Internet
Author: User

//Local notification callback function that is called when the application is in the foreground- (void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) Notification {NSLog (@"noti:%@", notification); //where the real need to deal with interaction here//get the data with the notificationNSString *key =[Notification.userinfo Objectforkey:localpushkey]; NSString*title = [Notification.userinfo objectforkey:@"title"]; NSString*content = [Notification.userinfo objectforkey:@"content"]; NSString*sysversion =[Uidevice currentdevice].systemversion; CGFloat version=[Sysversion Floatvalue]; if(Version >=8.0) {Uialertcontroller*alertcontroller =[Uialertcontroller alertcontrollerwithtitle:title message:content Preferredstyle:uialertcontrollerstylealert]        ; Uialertaction*okaction = [Uialertaction actionwithtitle:@"OK"Style:uialertactionstyledefault Handler:nil];        [Alertcontroller addaction:okaction]; [[UIApplication Sharedapplication].keywindow.rootviewcontroller Presentviewcontroller:alertcontroller Animated:    YES Completion:nil]; }    Else{Uialertview*alert =[[Uialertview alloc] Initwithtitle:title message:content Delegate: nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil];    [Alert show]; }    //update the number of badges displayedNsinteger badge =[UIApplication Sharedapplication].applicationiconbadgenumber; Badge--; Badge= Badge >=0? Badge:0; [UIApplication Sharedapplication].applicationiconbadgenumber=badge; //You can cancel the push when no more push is required[AppController Cancellocalnotificationwithkey:key];}//set up local notifications+ (void) Registerlocalnotification: (NSNumber *) alerttime key: (NSString *) key alertbody: (NSString *) alertbody title: (NSString *) title content: (NSString *) content{//NSLog (@ "registerlocalnotification:%long%@%@%@%@", [Alerttime IntegerValue], key, Alertbody, title, cont ENT);Uilocalnotification *notification =[[Uilocalnotification alloc] init]; //set the time to trigger notificationsNSDate *firedate =[NSDate datewithtimeintervalsincenow:[alerttime IntegerValue]; //NSLog (@ "firedate=%@", firedate);notification.firedate=firedate; //Time ZoneNotification.timezone =[Nstimezone Defaulttimezone]; //set the recurrence intervalNotification.repeatinterval =Kcfcalendarunitsecond; //Notification Content//notification.alerttitle = @ "Notification";Notification.alertbody =Alertbody; Notification.applicationiconbadgenumber=1; //sounds that are played when notifications are triggeredNotification.soundname =Uilocalnotificationdefaultsoundname; //Notification ParametersNsdictionary *userdict = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:key, title, content, nil] Forkeys:[nsarray Arraywithobjects:localpushkey,@"title",@"content", Nil]];//[Nsdictionary dictionarywithobject:userdictdata Forkey:localpushkey];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];}//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 *userkey =[UserInfo Objectforkey:localpushkey]; //If you find a notification that you want to cancel, cancel            if(UserKey! = Nil &&[UserKey Isequaltostring:key])                {[[UIApplication sharedapplication] cancellocalnotification:notification];  Break; }        }    }}

iOS local push

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.