推送
2016-03-28 20:34
225 查看
今天我给大家讲讲,关于本地推送通知的使用
1、远程推送(Remote Notification)
2、本地推送 (Local Notification)
推送通知就是该app无论是在使用中,还是没有没有使用,都可以通过这个通知告诉使用者,它喜欢的app的相关消息。
注意:发送推送通知的时候,如果APP在前台运行,那么推送的通知不会被呈现出来
推送都能如期发出,但是用户 不一定能如期去接收。
UIUserNotificationSettings推送通知的类
通知的几种格式,可以同时一起放在字典中
UIUserNotificationTypeNone = 0, 没有样式
UIUserNotificationTypeBadge = 1 << 0,右上角
UIUserNotificationTypeSound = 1 << 1,有声音
UIUserNotificationTypeAlert = 1 << 2,弹出
首先我们都先在AppDelegate中写
1、查询该设备的版本(返回的是double类型):
[UIDevice currentDevice].systemVersion.doubleValue
UIUserNotificationSettings *set = [UIUserNotificationSettings
settingsForTypes:
UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:nil];
[application
registerUserNotificationSettings:set];
if
(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILabel
*label =[[UILabel
alloc]init];
label.frame
=CGRectMake(0,
300,
300,
300);
label.text
=[NSString
stringWithFormat:@"%@",launchOptions];
label.font
=[UIFont
systemFontOfSize:14];
label.numberOfLines
=0;
[self.window.rootViewController.view
addSubview:label];
}
//接到通知走这
-(void)application:(UIApplication
*)application didReceiveLocalNotification:(UILocalNotification
*)notification{
UILabel
*label =[[UILabel
alloc]init];
label.frame
=CGRectMake(0,
300,
300,
300);
//label.text =[NSString stringWithFormat:@"%@",launchOptions];
label.font
=[UIFont
systemFontOfSize:14];
label.numberOfLines
=
0;
label.backgroundColor
= [UIColor
blackColor];
[self.window.rootViewController.view
addSubview:label];
}
在viewController中写
UILocalNotification
*loc = [UILocalNotification
new];
loc.fireDate
= [NSDate
dateWithTimeIntervalSinceNow:3];
//
具体内容
loc.alertBody
=
@"dddddd";
//
允许运行
loc.hasAction
=
YES;
//
弹出框的名字
loc.alertAction
=
@"ssssss";
//
角标的显示
loc.applicationIconBadgeNumber
=
5;
//
给声音
loc.soundName
=
UILocalNotificationDefaultSoundName;
[[UIApplication
sharedApplication]
scheduleLocalNotification:loc];
[[UIApplication
sharedApplication] setApplicationIconBadgeNumber:0];
移除通知
[[UIApplication
sharedApplication]cancelAllLocalNotifications];
1、远程推送(Remote Notification)
2、本地推送 (Local Notification)
推送通知就是该app无论是在使用中,还是没有没有使用,都可以通过这个通知告诉使用者,它喜欢的app的相关消息。
注意:发送推送通知的时候,如果APP在前台运行,那么推送的通知不会被呈现出来
推送都能如期发出,但是用户 不一定能如期去接收。
UIUserNotificationSettings推送通知的类
通知的几种格式,可以同时一起放在字典中
UIUserNotificationTypeNone = 0, 没有样式
UIUserNotificationTypeBadge = 1 << 0,右上角
UIUserNotificationTypeSound = 1 << 1,有声音
UIUserNotificationTypeAlert = 1 << 2,弹出
首先我们都先在AppDelegate中写
1、查询该设备的版本(返回的是double类型):
[UIDevice currentDevice].systemVersion.doubleValue
UIUserNotificationSettings *set = [UIUserNotificationSettings
settingsForTypes:
UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:nil];
[application
registerUserNotificationSettings:set];
if
(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILabel
*label =[[UILabel
alloc]init];
label.frame
=CGRectMake(0,
300,
300,
300);
label.text
=[NSString
stringWithFormat:@"%@",launchOptions];
label.font
=[UIFont
systemFontOfSize:14];
label.numberOfLines
=0;
[self.window.rootViewController.view
addSubview:label];
}
//接到通知走这
-(void)application:(UIApplication
*)application didReceiveLocalNotification:(UILocalNotification
*)notification{
UILabel
*label =[[UILabel
alloc]init];
label.frame
=CGRectMake(0,
300,
300,
300);
//label.text =[NSString stringWithFormat:@"%@",launchOptions];
label.font
=[UIFont
systemFontOfSize:14];
label.numberOfLines
=
0;
label.backgroundColor
= [UIColor
blackColor];
[self.window.rootViewController.view
addSubview:label];
}
在viewController中写
UILocalNotification
*loc = [UILocalNotification
new];
loc.fireDate
= [NSDate
dateWithTimeIntervalSinceNow:3];
//
具体内容
loc.alertBody
=
@"dddddd";
//
允许运行
loc.hasAction
=
YES;
//
弹出框的名字
loc.alertAction
=
@"ssssss";
//
角标的显示
loc.applicationIconBadgeNumber
=
5;
//
给声音
loc.soundName
=
UILocalNotificationDefaultSoundName;
[[UIApplication
sharedApplication]
scheduleLocalNotification:loc];
[[UIApplication
sharedApplication] setApplicationIconBadgeNumber:0];
移除通知
[[UIApplication
sharedApplication]cancelAllLocalNotifications];
相关文章推荐
- 文件的打开和关闭
- 【转】 java web开发之安全事项
- hdu 5637 Transform 最短路
- TCP面试题(一)之TCP的三次握手和accept()的顺序
- 可能导致Loadrunner检查点中savecount为0的分析
- java设计模式之工厂模式
- linux课程笔记--基础部分
- mysql查看表结构的几种方式
- GLUT文档
- C++实验2-项目1标准体重
- java第三次作业
- Leetcode 233. Number of Digit One
- 20160326高项
- HDOJ 5651-xiaoxin juju needs help【字符的全排列】
- js窗口间通信--postMessage
- 构造函数不能调用构造函数
- centos6 静态ip设置
- 最大公约数和最小公倍数
- UVa 11361 Investigating Div-Sum Property
- Spring 源码分析(四) ——MVC(四)核心逻辑