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;
}