There are two kinds of message notifications on iOS, one for local Notification and one for remote messages (Push Notification, also known as Remote Notification), designed both to alert users Now something new has happened that will entice the user to reopen the app. Local push can also be controlled by the server, for example, if there is a new message, push the message, but the premise is that the program must be open, and remote push, is through the Apple APNs server, pushed to the phone, the mobile phone in the push to specific which program, generally remote push to use more, first introduce the next local push, The following section describes remote push.
Local push:
First, register in appdelegate first:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { [Application registerusernotificationsettings:[uiusernotificationsettings Settingsfortypes: Uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound categories:nil]];//Register Local push //Override point for customization after application launch . return YES;}
Then, push is implemented in the specific Viewcontroller:
-(Ibaction) Localpushnow: (ID) Sender {dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0) , ^{//local push uilocalnotification*notification = [[Uilocalnotification alloc]init]; NSDate * pushdate = [NSDate datewithtimeintervalsincenow:10]; if (notification! = nil) {notification.firedate = pushdate; Notification.timezone = [Nstimezone defaulttimezone]; Notification.repeatinterval = Kcfcalendarunitday; Notification.soundname = Uilocalnotificationdefaultsoundname; Notification.alertbody = @ "Hello,world"; Notification.applicationiconbadgenumber = 0; Nsdictionary*info = [nsdictionary dictionarywithobject:@ "test" forkey:@ "name"]; Notification.userinfo = info; [[UIApplication sharedapplication] schedulelocalnotification:notification]; } });}
The push message is received in Appdelegate:
// receive local push
Receive local push-(void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) notification{ NSLog (@ "%@", notification.alertbody); Uilabel*label = [[UILabel alloc]init]; Label.frame = CGRectMake (0, 0,.); Label.layer.cornerRadius = ten; Label.backgroundcolor = [Uicolor blackcolor]; Label.text = Notification.alertbody; Label.textcolor = [Uicolor whitecolor]; Label.font = [Uifont systemfontofsize:12]; Label.textalignment = Nstextalignmentcenter; [Self.window Addsubview:label];}
The following conditions may occur in the process:
attempting to schedule a local notification ... With a sound but haven ' t received permission from the user to play sounds
attempting to schedule a local notification ... With a alert but haven ' t received permission from the user to display alerts
It may be because you are not registered, or the settings do not have the push feature turned on,
Local push of iOS (push)