Local push for ios (push)

Source: Internet
Author: User

Local push for 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,

Related Article

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.