一、推送通知


(4)点击推送通知后,默认会自动打开发出推送通知的app
(5)不管app打开还是关闭,推送通知都能如期发出
二、本地推送通知
UILocalNotification *ln = [[UILocalNotification alloc] init];
(2)设置本地推送通知属性
property(nonatomic,copy) NSDate *fireDate;
@property(nonatomic,copy) NSString *alertBody;
@property(nonatomic,copy) NSString *alertAction;
@property(nonatomic,copy) NSString *soundName;
@property(nonatomic) NSInteger applicationIconBadgeNumber;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
获得被调度(定制)的所有本地推送通知
@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
(已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)
取消调度本地推送通知
- (void)cancelLocalNotification:(UILocalNotification *)notification; - (void)cancelAllLocalNotifications;
立即发出本地推送通知
- (void)presentLocalNotificationNow:(UILocalNotification *)notification;
(3) 本地推送通知的其他属性
每隔多久重复发一次推送通知
@property(nonatomic) NSCalendarUnit repeatInterval;
点击推送通知打开app时显示的启动图片
@property(nonatomic,copy) NSString *alertLaunchImage;
附加的额外信息
@property(nonatomic,copy) NSDictionary *userInfo;
时区
@property(nonatomic,copy) NSTimeZone *timeZone;
(一般设置为[NSTimeZone defaultTimeZone] ,跟随手机的时区)
(4)点击本地推送通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey取出本地推送通知对象
(5)iOS 8.0的变化
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; [application registerUserNotificationSettings:settings];
代码:
// 在iOS8之后,Apple对用户隐私要求更加严格,所有很多东西都需要请求用户权限
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
/*
UIUserNotificationTypeNone = 0, // 不展示通知
UIUserNotificationTypeBadge = 1 << 0, // 应用图标右上角的红色数字权限
UIUserNotificationTypeSound = 1 << 1, // 提示音的权限
UIUserNotificationTypeAlert = 1 << 2, // 弹窗或横幅的权限
*/
// 权限类型
UIUserNotificationType types = UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert;
// 用户通知设置
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:types categories:nil];
// 注册用户设置,用户的请求窗口会弹出一次
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
发送本地通知
// 1.创建本地通知对象
UILocalNotification *ln = [[UILocalNotification alloc] init];
// 2.设置本地通知属性
//************************************************************/
// 什么时候发送这个本地通知
ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 设置通知的内容
ln.alertBody = alertBody;
// 设置提示音,如果用户是静音,它自动会转为震动
ln.soundName = UILocalNotificationDefaultSoundName;
// 应用图标
ln.applicationIconBadgeNumber = 10;
// 当用用户点击通知时候,进入应用程序,要不要显示启动图标
ln.alertLaunchImage = @"UILaunchImageFile";
// 用户信息(比如界面跳转的信息)
ln.userInfo = userInfo;
//************************************************************/
// 调度本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
界面跳转
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 如果应用程序被杀死了,我们需要在这里处理界面跳转
// 1.从应用程序的加载项取出本地通知对象
UILocalNotification *ln = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// 2.如果存在这个对象,表示用户是点击本地通知进入到应用的
if (ln) {
// 跳转界面
[self jumpToPageWithLocalNotification:ln];
}
return YES;
}
/**
1.当收到本地通知就会调用该代理方法
调用场景
1.如果应用程序在后台,当点击通知的时候
2.如果应用程序在前台,一旦收到本地通知,就会调用该方法
3.如果应用程序被杀死了,那么这个方法就不再执行了
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
/*
UIApplicationStateActive, 活动
UIApplicationStateInactive, 活动与后台切换的一种状态
UIApplicationStateBackground 后台
*/
// 如果应用在前台,就不要跳转界面了
if (application.applicationState == UIApplicationStateActive) {
return;
}
// 跳转界面
[self jumpToPageWithLocalNotification:notification];
}
///跳转界面
- (void) jumpToPageWithLocalNotification:(UILocalNotification *) notification
{
// NSLog(@"%s",__func__);
// 取出用户信息
NSDictionary *userInfo = notification.userInfo;
// 拿到跳转到索引
NSInteger index = [userInfo[@"index"] integerValue];
// 获取根控制
UITabBarController *tabVc = (UITabBarController *)self.window.rootViewController;
// 跳转界面
tabVc.selectedIndex = index;
}
总结:
1.在发送本地通知的时候,通过userInfo属性来指示跳转到那个界面
2.监听本地通知的接收
1.当收到本地通知就会调用该代理方法
调用场景
1.如果应用程序在后台,当点击通知的时候
2.如果应用程序在前台,一旦收到本地通知,就会调用该方法
所以:判断如果应用程序在前台时候,不要执行界面跳转,来提高用户体验
3.如果应用程序被杀死了,那么这个方法就不再执行了
//- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
3.在 didFinishLaunchingWithOptions:方法中,完成界面跳转
判断用户是点击本地通知进入到应用的
1.从应用程序的加载项取出本地通知对象
UILocalNotification *ln = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
2.如果 ln 有值表示是用户点击本地通知进入的,这个时候,才需要进行界面跳转.
三、远程推送通知
(2)为什么需要远程推送通知?
(3)远程推送通知可以解决以上问题
2、远程推送通知使用须知
(2)长连接的作用
(3)长连接的好处
(1)开发iOS程序的推送功能, iOS端需要做的事
1.请求苹果获得deviceToken
2.得到苹果返回的deviceToken
3.发送deviceToken给公司的服务器
4.监听用户对通知的点击
(2)调试iOS的远程推送功能, 必备条件:
1.真机
2.调试推送需要的证书文件
1> aps_development.cer : 某台电脑就能调试某个app的推送服务
2> ios_development.cer : 让电脑具备真机调试的能力(调试设备)
3> iphone5_qq.mobileprovision : 某台电脑就能利用某台设备调试某个程序
(3)发布具有推送服务的app
1> aps_production.cer : 如果发布的程序中包含了推送服务,就必须安装这个证书
2> ios_distribution.cer : 让电脑具备发布程序的能力
3> qq.mobileprovision : 某台电脑就能发布某个程序
4、注册远程推送通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 注册远程通知
UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];
return YES;
}
如果是第一次注册,会弹出右边的对话框

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@", deviceToken);
}
5、点击远程推送通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
app已经被关闭(进程已死)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; launchOptions参数通过UIApplicationLaunchOptionsRemoteNotificationKey取出服务器返回的字典内容
6、PushMeBaby





四、JPush
(2)JPush的集成步骤




