消息推送(本地推送)

zone 时区 badge 图标

//iOS8之后需要注册通知类型包含哪些(声音,图标文字,文本)信息

/*

UIUserNotificationTypeNone    = 0,

UIUserNotificationTypeBadge   = 1 << 0,包含图标文字0001

UIUserNotificationTypeSound   = 1 << 1, //声音0010

UIUserNotificationTypeAlert   = 1 << 2, //主题内容0100

*/

//0111

UIUserNotificationSettings*setting = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil];

[[UIApplicationsharedApplication]registerUserNotificationSettings:setting];

//1.创建一个本地通知

UILocalNotification*local= [[UILocalNotificationalloc]init];

// timer-based scheduling定制特定的时间发出通知

@property(nonatomic,copy) NSDate *fireDate;触发时间

@property(nonatomic,copy) NSTimeZone *timeZone;时区

@property(nonatomic) NSCalendarUnit repeatInterval;重复间隔// 0 means don't repeat

@property(nonatomic,copy) NSCalendar *repeatCalendar;重复间隔

@property(nonatomic,copy) CLRegion *regionNS_AVAILABLE_IOS(8_0);//区域

@property(nonatomic,assign)BOOLregionTriggersOnceNS_AVAILABLE_IOS(8_0);//决定区域的一个bool值

// alerts

@property(nonatomic,copy) NSString *alertBody;提醒的主题

@property(nonatomic)BOOLhasAction;// NO不显示滑动解锁的按钮反之显示

@property(nonatomic,copy) NSString *alertAction;//滑动解锁的文字

@property(nonatomic,copy) NSString *alertLaunchImage;//点击通知横幅的启动程序的启动图片

@property(nonatomic,copy) NSString *alertTitle提示的标题文字

// sound默认: UILocalNotificationDefaultSoundName

@property(nonatomic,copy) NSString *soundName;

// badge

@property(nonatomic) NSInteger applicationIconBadgeNumber;//图标文字

// user info

@property(nonatomic,copy) NSDictionary *userInfo;//用户指定的携带参数

[UIUserNotificationSettings settingsForUserNotificationTypes:userNotificationActionSettings:]

@property(nonatomic, copy) NSString *categoryNS_AVAILABLE_IOS(8_0);分类

//设置属性

local.alertBody=@"女神:在吗?";

local.fireDate= [NSDatedateWithTimeIntervalSinceNow:5];

local.soundName=UILocalNotificationDefaultSoundName;

local.alertAction=@"约";

local.applicationIconBadgeNumber=8;

local.userInfo=@{@"name":@"女神",@"content":@"在吗?",@"time":@"20180815"};

//应用级别

//定制一个通知用代理监听什么接收到通知

[[UIApplicationsharedApplication]scheduleLocalNotification:local];

//立即发出通知

[[UIApplicationsharedApplication]presentLocalNotificationNow:local];

//取消所有的本地通知

[[UIApplicationsharedApplication]cancelAllLocalNotifications];

/**

*接收到本地通知的时候就会调用

*程序在前台:会调用程序在后台:点击横幅就算接收通知也会调用->程序活着就会调用

*@paramapplication应用

*@paramnotification通知

*/

- (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification

{

//调试:

//1>控制台

NSLog(@"%@",notification);

//2>添加控件

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,320,300)];

label.backgroundColor= [UIColorredColor];

label.text= [NSStringstringWithFormat:@"%@",notification];

[self.window.rootViewController.viewaddSubview:label];

//跳转对应的界面

}

/**

*程序从死到生程序启动:1>点击图标启动2>接收通知(点击横幅)

*

*@paramapplication   <#application description#>

*@paramlaunchOptions <#launchOptions description#>

*

*@return<#return value description#>

*/

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

//UIApplicationLaunchOptionsLocalNotificationKey接收到本地通知才会来到这里

UILocalNotification*info = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

if(info) {

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,320,300)];

label.backgroundColor= [UIColororangeColor];

[self.window.rootViewController.viewaddSubview:label];

//取出携带信息

label.numberOfLines=0;

//NSDictionary *dict = info[@"UIConcreteLocalNotification"];

// NSArray *keys = [info allKeys];

//for (NSString *str in keys) {

//   label.text = [NSString stringWithFormat:@"%@",str];

//}

//NSDictionary *dict1 = dict[@"user info"];

label.text= [NSStringstringWithFormat:@"%@",info.userInfo];

//跳转对应的界面

}

returnYES;

}

你可能感兴趣的:(消息推送(本地推送))