#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /* // timer-based scheduling @property(nullable, nonatomic,copy) NSDate *fireDate; 触发时间 // the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. for an egg timer). // pass a time zone to interpret fireDate as a wall time to be adjusted automatically upon time zone changes (e.g. for an alarm clock). @property(nullable, nonatomic,copy) NSTimeZone *timeZone; 时区 @property(nonatomic) NSCalendarUnit repeatInterval; 重复次数 // 0 means don't repeat @property(nullable, nonatomic,copy) NSCalendar *repeatCalendar; // location-based scheduling // set a CLRegion object to trigger the notification when the user enters or leaves a geographic region, depending upon the properties set on the CLRegion object itself. registering multiple UILocalNotifications with different regions containing the same identifier will result in undefined behavior. the number of region-triggered UILocalNotifications that may be registered at any one time is internally limited. in order to use region-triggered notifications, applications must have "when-in-use" authorization through CoreLocation. see the CoreLocation documentation for more information. @property(nullable, nonatomic,copy) CLRegion *region 当进入设置区域内 发送通知 (NS_AVAILABLE_IOS(8_0); // when YES, the notification will only fire one time. when NO, the notification will fire every time the region is entered or exited (depending upon the CLRegion object's configuration). default is YES. @property(nonatomic,assign) BOOL regionTriggersOnce 进入区域内只提醒一次,下次就不提醒NS_AVAILABLE_IOS(8_0); // alerts @property(nullable, nonatomic,copy) NSString *alertBody; 弹窗主体 // defaults to nil. pass a string or localized string key to show an alert @property(nonatomic) BOOL hasAction; 在界面通知中心中滑块和按钮 // defaults to YES. pass NO to hide launching button/slider @property(nullable, nonatomic,copy) NSString *alertAction; // used in UIAlert button or 'slide to unlock...' slider in place of unlock @property(nullable, nonatomic,copy) NSString *alertLaunchImage; 当luanch 按钮被点击后的luanch图片 // used as the launch image (UILaunchImageFile) when launch button is tapped @property(nullable, nonatomic,copy) NSString *alertTitle 弹窗主题NS_AVAILABLE_IOS(8_2); // defaults to nil. pass a string or localized string key // sound @property(nullable, nonatomic,copy) NSString *soundName; 播放声音文件 // name of resource in app's bundle to play or UILocalNotificationDefaultSoundName // badge @property(nonatomic) NSInteger applicationIconBadgeNumber; app的角标 // 0 means no change. defaults to 0 // user info @property(nullable, nonatomic,copy) NSDictionary *userInfo; 本地通知配置的信息 // throws if contains non-property list types // category identifer of the local notification, as set on a UIUserNotificationCategory and passed to +[UIUserNotificationSettings settingsForTypes:categories:] //分类 @property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0); */ /* UIUserNotificationTypeNone = 0, 没有 UIUserNotificationTypeBadge = 1 << 0, 角标 UIUserNotificationTypeSound = 1 << 1, 通知可以有声音 UIUserNotificationTypeAlert = 1 << 2, 通知有弹窗 */ //设置分类 UIMutableUserNotificationCategory * category = [[UIMutableUserNotificationCategory alloc]init]; category.identifier = @"cate"; /* // The unique identifier for this action. @property (nullable, nonatomic, copy) NSString *identifier; 标识 // The localized title to display for this action. @property (nullable, nonatomic, copy) NSString *title; 标题 // The behavior of this action when the user activates it. @property (nonatomic, assign) UIUserNotificationActionBehavior behavior 行为 NS_AVAILABLE_IOS(9_0); // Parameters that can be used by some types of actions. @property (nonatomic, copy) NSDictionary *parameters NS_AVAILABLE_IOS(9_0); // How the application should be activated in response to the action. @property (nonatomic, assign) UIUserNotificationActivationMode 模式 activationMode; // Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored. @property (nonatomic, assign, getter=isAuthenticationRequired) BOOL authenticationRequired; // Whether this action should be indicated as destructive when displayed. @property (nonatomic, assign, getter=isDestructive) BOOL destructive; */ UIMutableUserNotificationAction * action = [[UIMutableUserNotificationAction alloc]init]; action.title = @"哈哈"; action.identifier = @"id1"; action.activationMode = UIUserNotificationActivationModeBackground; action.authenticationRequired = NO; action.destructive = YES; //设置了动作的行为问为文本输入模式, action.behavior = UIUserNotificationActionBehaviorTextInput; UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc]init]; //设置动作属性 action1.title = @"嘻嘻嘻"; action1.identifier = @"id2"; action1.activationMode = UIUserNotificationActivationModeBackground; action1.authenticationRequired = NO; action1.destructive = YES;//设置动作按钮的颜色, yes 为红色, no 为蓝色 [category setActions:@[action,action1] forContext:UIUserNotificationActionContextDefault]; // | 位或 << 左移 UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone | UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObjects:category, nil]]; //注册用户通知 [[UIApplication sharedApplication]registerUserNotificationSettings:settings]; } //点击屏幕定制通知 5 秒后发送通知 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //创建本地通知 (iOS 8.0 之后,注册通知需要注册用户通知) UILocalNotification * localNotification = [[UILocalNotification alloc]init]; //配置通知 //5秒后发送通知 localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; localNotification.alertBody = @"Zhq:在吗?"; localNotification.alertTitle = @"一款牛逼的聊天软件"; localNotification.applicationIconBadgeNumber = 10; localNotification.alertAction = @"聊天中"; localNotification.alertAction =@"hahahahha"; localNotification.hasAction = NO; localNotification.category = @"cate"; localNotification.userInfo = @{@"name":@"xiaohua"}; //定制通知 [[UIApplication sharedApplication]scheduleLocalNotification:localNotification]; //系统展示通知 }
#import "AppDelegate.h" #import "jumpViewController.h" @interface AppDelegate () @end @implementation AppDelegate //接收到本地通知是调用 (应用的前后台都会执行这个方法 , 当应用程序挂了就不会执行这个方法) -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"didReceiveLocalNotification"); if (![[NSUserDefaults standardUserDefaults]boolForKey:@"Foreground"]) { [self jumpVc]; } } -(void)jumpVc { UIStoryboard * sb = [UIStoryboard storyboardWithName:@"jump" bundle:nil]; jumpViewController * jv = [sb instantiateInitialViewController]; self.window.rootViewController = jv; } //点击本地通知Action就会执行下面两个方法 如果实现withResponseInfo方法,此方法就不会调用 -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ //通过Action 的idetifier 来分别执行不同的业务逻辑 NSLog(@"i%@" , identifier); completionHandler(); } //NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED,当设置action的行为为textInput 就可以调用这个方法,通过responseInfo获取用糊输入的内容,还做其他业务逻辑 -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{ NSLog(@"%@",responseInfo[@"UIUserNotificationActionResponseTypedTextKey"]); completionHandler(); } /远程通知的Action按钮被点击后执行 , 与本地通知一样 //-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{ // //} //
//
远程通知
当设置action的行为为textInput 就可以调用这个方法,通过responseInfo获取用糊输入的内容,还做其他业务逻辑
//-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{ // //}
//应用程序第一次启动的时候就会调用didFinishLaunchingWithOptions.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILabel * lab = [[UILabel alloc]initWithFrame:(CGRect){0,100,300,300}];
lab.backgroundColor = [UIColor orangeColor];
lab.numberOfLines = 0; NSLog(@"%@" , [NSString stringWithFormat:@"%@",launchOptions]);
lab.text = [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]];
[self.window.rootViewController.view addSubview:lab]; NSLog(@"didFinishLaunchingWithOptions");
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey])
{ [self jumpVc]; }
如何发送本地通知:
UILocalNotification *ln = [[UILocalNotification alloc] init];
设置本地推送通知属性
@property(nonatomic,copy) NSDate *fireDate;
@property(nonatomic,copy) NSString *alertBody;
@property(nonatomic,copy) NSString *alertAction;
@property(nonatomic,copy) NSString *soundName;
@property(nonatomic) NSInteger applicationIconBadgeNumber;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
(已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)
- (void)cancelLocalNotification:(UILocalNotification *)notification;
- (void)cancelAllLocalNotifications;
- (void)presentLocalNotificationNow:(UILocalNotification *)notification;
本地通知的其他属性:
@property(nonatomic) NSCalendarUnit repeatInterval;
@property(nonatomic,copy) NSString *alertLaunchImage;
@property(nonatomic,copy) NSDictionary *userInfo;
@property(nonatomic,copy) NSTimeZone *timeZone;
(一般设置为[NSTimeZone defaultTimeZone] ,跟随手机的时区)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:settings];