本地通知 UILocalNotification
来源:互联网 发布:spycall在淘宝怎么买 编辑:程序博客网 时间:2024/05/02 01:43
1.如果只是纯粹的通知下面的代码就可以实现了
- (void)viewDidLoad {
[superviewDidLoad];
[self SendLocalNotification];
}
-(void)SendLocalNotification{
UILocalNotification *localNotification = [[UILocalNotificationalloc]init];
//触发通知时间
localNotification.fireDate = [NSDatedateWithTimeIntervalSinceNow:0.1];
//重复间隔
// localNotification.repeatInterval = kCFCalendarUnitMinute;
localNotification.timeZone = [NSTimeZonedefaultTimeZone];
//通知内容
localNotification.alertBody =@"家里老人摔倒了";
localNotification.applicationIconBadgeNumber =1;
localNotification.soundName =UILocalNotificationDefaultSoundName;
//通知参数
localNotification.userInfo =@{@"kLocalNotificationKey":@"去吧,皮卡丘"};
localNotification.category =@"kNotificationCategoryIdentifile";
[[UIApplicationsharedApplication]scheduleLocalNotification:localNotification];
}
2.如果还想增加赞和评论的功能继续添加下面代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[selfregisterLocalNotification];
returnYES;
}
- (void)registerLocalNotification
{
//创建消息上面要添加的动作
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationActionalloc]init];
action1.identifier =kNotificationActionIdentifileStar;
action1.title =@"赞";
//当点击的时候不启动程序,在后台处理
action1.activationMode =UIUserNotificationActivationModeBackground;
//需要解锁才能处理(意思就是如果在锁屏界面收到通知,并且用户设置了屏幕锁,用户点击了赞不会直接进入我们的回调进行处理,而是需要用户输入屏幕锁密码之后才进入我们的回调),如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
action1.authenticationRequired =YES;
/*
destructive属性设置后,在通知栏或锁屏界面左划,按钮颜色会变为红色
如果两个按钮均设置为YES,则均为红色(略难看)
如果两个按钮均设置为NO,即默认值,则第一个为蓝色,第二个为浅灰色
如果一个YES一个NO,则都显示对应的颜色,即红蓝双色 (CP色)。
*/
action1.destructive =NO;
//第二个动作
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationActionalloc]init];
action2.identifier =kNotificationActionIdentifileComment;
action2.title =@"评论";
//当点击的时候不启动程序,在后台处理
action2.activationMode =UIUserNotificationActivationModeBackground;
//设置了behavior属性为 UIUserNotificationActionBehaviorTextInput的话,则用户点击了该按钮会出现输入框供用户输入
action2.behavior =UIUserNotificationActionBehaviorTextInput;
//这个字典定义了当用户点击了评论按钮后,输入框右侧的按钮名称,如果不设置该字典,则右侧按钮名称默认为 “发送”
action2.parameters =@{UIUserNotificationTextInputActionButtonTitleKey:@"评论"};
//创建动作(按钮)的类别集合
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategoryalloc]init];
//这组动作的唯一标示
category.identifier =kNotificationCategoryIdentifile;
//最多支持两个,如果添加更多的话,后面的将被忽略
[categorysetActions:@[action1, action2]forContext:(UIUserNotificationActionContextMinimal)];
//创建UIUserNotificationSettings,并设置消息的显示类类型
UIUserNotificationSettings *uns = [UIUserNotificationSettingssettingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound)categories:[NSSetsetWithObject:category]];
[[UIApplicationsharedApplication]registerUserNotificationSettings:uns];
}
//本地通知回调函数,当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"%@", notification.userInfo);
[selfshowAlertView:@"用户没点击按钮直接点的推送消息进来的/或者该app在前台状态时收到推送消息"];
NSInteger badge = [UIApplicationsharedApplication].applicationIconBadgeNumber;
badge -= notification.applicationIconBadgeNumber;
badge = badge >=0 ? badge :0;
[UIApplicationsharedApplication].applicationIconBadgeNumber = badge;
}
//在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullableNSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler
{
if ([identifierisEqualToString:kNotificationActionIdentifileStar]) {
[selfshowAlertView:@"点了赞"];
}elseif ([identifierisEqualToString:kNotificationActionIdentifileComment]) {
[selfshowAlertView:[NSStringstringWithFormat:@"用户评论为:%@", responseInfo[UIUserNotificationActionResponseTypedTextKey]]];
}
completionHandler();
}
- (void)showAlertView:(NSString *)message
{
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:nilmessage:messagepreferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];
[alertaddAction:action];
[self.window.rootViewControllershowDetailViewController:alertsender:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[UIApplicationsharedApplication].applicationIconBadgeNumber =0;
}
- 本地通知UILocalNotification
- UILocalNotification 本地通知
- iOS本地通知UILocalNotification
- IOS 本地通知 UILocalNotification
- ios本地通知UILocalNotification
- 本地通知UILocalNotification
- iOS UILocalNotification本地通知
- 本地通知UILocalNotification
- iOS本地通知:UILocalNotification
- UILocalNotification本地通知介绍
- 本地通知UILocalNotification
- UILocalNotification 本地通知
- 本地通知UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- UILocalNotification一本地通知
- iOS本地通知:UILocalNotification
- 本地通知UILocalNotification
- drawable文件夹详解
- poj 1693 模拟
- 面试题41:和为s的两个数字VS和为s的连续正数序列
- JSON
- 【杭电-oj】-1002-A + B Problem II(大数相加)
- 本地通知 UILocalNotification
- 移植apache-1.3.39到arm平台
- git不区分大小写
- 二维码的生成 lizbqrencode遇到的问题
- 【链表】C++链表反转、链表逆序打印
- xmpp配置服务器
- Ideal and minimum layout viewports per device(常见设备的最小布局视口和理想视口一览表)
- 从无到有,从动态链编到静态链编,与清华大学合作完成了一个Qt项目
- O(n) 方法求数列极差