Explanation of local notifications/local notifications in iOS Han junqiang's blog

Source: Internet
Author: User

Explanation of local notifications/local notifications in iOS Han junqiang's blog

Notification is a very common information transfer mechanism in smartphone application programming. It can save resources and check information status without consuming resources ), apps in iOS are divided into two different Notification types: local and remote. Local Notification is centrally managed by NotificationManager in iOS. You only need to add encapsulated local Notification objects to the queue of the system Notification management mechanism. The system will trigger local Notification at the specified time, the application only needs to design the method for processing Notification to complete the whole Notification process.

The object used by local Notification is UILocalNotification. The properties of UILocalNotification cover all the content required to process Notification. UILocalNotification attributes include fireDate, timeZone, repeatInterval, repeatCalendar, alertBody, alertAction, hasAction, alertLaunchImage, applicationIconBadgeNumber, soundName, and userInfo.

1. First, you need to understand the difference between the simulator and the real machine: The simulator will not receive audio prompts, And the simulator will not detect the possibility of receiving notifications. So let me add a few points:

1. Add monitoring notification:

 

 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){                [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];    }

Code:

 

 

# Import "ViewController. h "# import" DetailViewController. h "@ interface ViewController () @ property (weak, nonatomic) IBOutlet UIButton * schedule; @ property (weak, nonatomic) IBOutlet UIButton * unSchedule; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} // dispatch notification-(IBAction) schedule :( UIButton *) sender {// 1. create notification UILocalNotification * ln = [[UILocalNotification alloc] init]; if (ln) {// set the time zone ln. timeZone = [NSTimeZone defaultTimeZone]; // notification of the first time ln. fireDate = [[NSDate date] dateByAddingTimeInterval: 5]; // 2. set notification attributes ln. soundName = @ "click.wav"; // audio file name // the specific content of the notification ln. alertBody = @ "major news: the blog of Han GE has been updated. Come in and check it out !.... "; // Title displayed on the screen lock page. Complete title: (" slide "+ title) ln. alertAction = @ "view News"; // set the app icon number ln. applicationIconBadgeNumber = 10; // set the additional information of the app ln. userInfo = @ {@ "icon": @ "text.png", @ "title": @ "major news", @ "time": @ "2016-02-28", @ "body ": @ "major news: Han GE's blog has been updated. Come in and check it out! "}; // Set to restart the picture ln. alertLaunchImage = @ "101339g76j7j9t2zgzdvkj.jpg"; // set the interval for repeatedly sending notifications. // ln. repeatInterval = NSCalendarUnitMinute; // 3. scheduling notification (start a task and send a notification at the specified time) [[[UIApplication sharedApplication] scheduleLocalNotification: ln]; // it is meaningless to directly send a notification // [[UIApplication sharedApplication] presentLocalNotificationNow: ln] ;}}-(IBAction) noSchedule :( UIButton *) sender {// [[UIApplication sharedApplication] cancelalllocalconfigurications]; // notifications that have been issued and expired will be automatically removed from the array. NSArray * notes = [UIApplication sharedApplication]. scheduledlocalconfigurications; NSLog (@ "% @", notes);}-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( UILocalNotification *) note {DetailViewController * detailVC = segue. destinationViewController; detailVC. userInfo = note. userInfo;} @ end

2. Set Basic Attributes on the Notification Details page:

.h#import 
 
  @interface DetailViewController : UIViewController@property (nonatomic, strong) NSDictionary *userInfo;@end.m#import "DetailViewController.h"@interface DetailViewController ()@property (weak, nonatomic) IBOutlet UILabel *userInfoContent;@end@implementation DetailViewController- (void)viewDidLoad {    [super viewDidLoad];        self.userInfoContent.text = self.userInfo[@"body"];}- (void)setUserInfo:(NSDictionary *)userInfo{    _userInfo = userInfo;}@end
 

3. Real-time Monitoring of didFinishLaunchingWithOptions:

 

 

-(BOOL) application :( UIApplication *) application events :( NSDictionary *) launchOptions {// register a local notification if ([UIApplication instancesRespondToSelector: @ selector (registerusericationicationsettings :)]) {[application registerusernotifsettings: [UIUserNotificationSettings failed: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories: nil];} // NSLog (@ "----- tags ---"); UILabel * label = [[UILabel alloc] init]; label. frame = CGRectMake (0, 64,320,100); label. backgroundColor = [UIColor redColor]; label. font = [UIFont systemFontOfSize: 11]; label. numberOfLines = 0; label. textColor = [UIColor whiteColor]; label. text = [launchOptions description]; [[self. window. rootViewController. childViewControllers firstObject] view] addSubview: label]; UILocalNotification * note = launchOptions [UIApplicationLaunchOptionsURLKey]; if (note) {label. text = @ "click the program started by local notification";} else {label. text = @ "directly click the app icon to start the program";} self. label = label; return YES;}/*** it is called when the user clicks a local notification to enter the app (the app was not closed at the time) * If the app is disabled, this method is not called */-(void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {self. label. text = @ "click the notification to return to the foreground again"; ViewController * homeVC = [self. window. rootViewController. childViewControllers firstObject]; // [homeVC implements mseguewithidentifier: @ "toHome" sender: notification]; [homeVC implements mseguewithidentifier: @ "toHome" sender: notification];}
Three situations: (important)

1. The program runs in the background

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.