标签:
本地推送:只有程序在后台运行 或者退出以后才会收到通知并显示
程序在前台的时候,接收通知推送,但不显示
基本步骤:
1>> 使用本地通知之前 先注册usersettings
2>> 创建本地通知
3>> 设置本地通知属性
4>> 定制通知
1.UIUserNotificationType:
UIUserNotificationTypeAlert: 显示提示信息
UIUserNotificationTypeBadge: 显示图标信息
UIUserNotificationTypeSound: 声音提示
UIUserNotificationTypeNone : 什么都没有
2. UILocalNotification属性:
// 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 *region NS_AVAILABLE_IOS(8_0); 区域
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
// alerts
@property(nonatomic,copy) NSString *alertBody; // 提示主体信息
@property(nonatomic) BOOL hasAction; // YES 是否显示锁屏状态下地滑动按钮的提示信息
@property(nonatomic,copy) NSString *alertAction; //滑动来解锁 提示信息
@property(nonatomic,copy) NSString *alertLaunchImage;//是否显示 启动图片
@property(nonatomic,copy) NSString *alertTitle 标题
// sound :UILocalNotificationDefaultSoundName
@property(nonatomic,copy) NSString *soundName; // name of resource in app‘s bundle to play or UILocalNotificationDefaultSoundName
// badge
@property(nonatomic) NSInteger applicationIconBadgeNumber;改变图标文字
// user info
@property(nonatomic,copy) NSDictionary *userInfo; // 携带信息
// category of the local notification, as passed to +[UIUserNotificationSettings settingsForUserNotificationTypes:userNotificationActionSettings:]
@property (nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);
3.示例:
在viewController.m中
//0.使用本地通知之前 先注册usersettings
//注册通知类型 包含 图标 声音 提示信息
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 1.创建本地通知
UILocalNotification *local = [[UILocalNotification alloc] init];
//2.设置本地通知属性
//2.1触发时间
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
//2.2显示滑动到应用的提示
local.hasAction = YES;
//2.3是否显示启动图片
local.alertLaunchImage = @"随便写";
//2.4 显示‘滑动解锁到XX‘
local.alertAction = @"聊天";
//2.5通知的内容
local.alertBody = @"你好,在吗?";
//2.6
local.alertTitle = @"标题";
//2.7图标数字
local.applicationIconBadgeNumber = 10;
//3.定制通知
[[UIApplication sharedApplication]scheduleLocalNotification:local];
在APPDelegate.m中,实现两个方法
@implementation AppDelegate
/**
* 接收到本地通知的时候就会调用
* 程序是活的
* :(前台) 默认不显示横幅的时候也会接收 通知
* (后台) 默认不接收 但是 点击了横幅 就相当于接收了通知
*@param application 应用
* @param notification 本地通知
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//添加控件
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
label.backgroundColor = [UIColor redColor];
label.numberOfLines = 0;
label.text = [NSString stringWithFormat:@"%@",notification];
[self.window.rootViewController.view addSubview:label];
NSLog(@"%@",notification);
}
/**
* 这个方法 程序 从死到生 就调用
* 程序是死的
* 1>点击图标 2>点击横幅(接收通知) 会调用
* @param application 应用
* @param launchOptions 启动参数字典
*
* @return <#return value description#>
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//如果UIApplicationLaunchOptionsLocalNotificationKey 有值就是接收到通知 调用的这个方法
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
//添加控件
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 500)];
label.backgroundColor = [UIColor redColor];
label.numberOfLines = 0;
//NSDictionary *dict = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
label.text = [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]];
[self.window.rootViewController.view addSubview:label];
//跳转到聊天界面
// 展示当前的聊天信息
//在吗?
//NSLog(@"%@", dict[@"userInfo"]);
}
return YES;
}
本地推送
标签:
原文地址:http://www.cnblogs.com/guozhenhu/p/4619612.html