Local push for ios (push) and ios push
There are two types of message notifications on iOS: Local Notification and Remote Notification ), these two notifications are designed to remind users of new things and attract users to re-open the application. Local push can also be controlled through the server. For example, if there is a new message, push the message, but the premise is that the program must be opened, and remote push is through the apple APNS server, which program is the mobile phone pushing to? Generally, remote push uses a lot. First, we will introduce local push. Next, we will introduce remote push.
Local push:
First, register in appdelegate:
-(BOOL) application :( UIApplication *) application attributes :( NSDictionary *) launchOptions {[application registerUserNotificationSettings: [UIUserNotificationSettings attributes: Attributes | UIUserNotificationTypeBadge | uiusericationicationtypesound categories: nil]; // register the local push // Override point for customization after application launch. return YES ;}
Then, implement push in the specific viewcontroller:
-(IBAction) localPushNow :( id) sender {dispatch_async (dispatch_get_global_queue (queue, 0), ^ {// push UILocalNotification * notification locally = [[UILocalNotification alloc] init]; NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow: 10]; if (notification! = Nil) {notification. fireDate = pushDate; notification. timeZone = [NSTimeZone defaultTimeZone]; notification. repeatInterval = kCFCalendarUnitDay; notification. soundName = uilocalnotificationdefasoundname; notification. alertBody = @ "hello, world"; notification. applicationIconBadgeNumber = 0; NSDictionary * info = [NSDictionary dictionaryWithObject: @ "test" forKey: @ "name"]; notification. userInfo = info; [[UIApplication sharedApplication] scheduleLocalNotification: notification] ;}});}
The push information will be 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,160, 20); label. layer. cornerRadius = 10; 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 situations may occur during the process:
Attempting to schedule a local notification ......With a sound but haven' t stored ed permission from the user to play sounds
Attempting to schedule a local notification ......With an alert but haven' t passed ed permission from the user to display alerts
It may be because you have not registered or the push function is not enabled in the settings,