推送(PUSH)

1、本地推送数组

1.创建本地推送安全

 1     UILocalNotification *notification = [[UILocalNotification alloc] init];
 2     //推送的内容
 3     notification.alertBody = @"起床了";//设备收到本地通知时横额或锁屏时的主要文字内容
 4     notification.alertAction = @"起床了(ˇˍˇ)";//锁屏时显示的slide to后面的文字内容
 5     //推送时间
 6     notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
 7     //多久重复一次
 8     notification.repeatInterval = NSCalendarUnitHour;
 9     //启动图片
10     notification.alertLaunchImage = @"AS_QQ@2x.png";
11     //收到推送声音
12     notification.soundName = UILocalNotificationDefaultSoundName;//默认的
13     notification.soundName = @"shake_sound_male.wav";//自定义的
14     //时区
15     notification.timeZone = [NSTimeZone localTimeZone];
16     //用户自定义数据
17     notification.userInfo = @{kNotificationKey:@"notification1"};
18     // 设置应用程序右上角的提醒个数
19     notification.applicationIconBadgeNumber++;
20     // 将通知添加到系统中
21     [[UIApplication sharedApplication] scheduleLocalNotification:notification];

注意这个方法只有在程序启动以后才会执行,所以当程序处于后台时,该方法不会执行。服务器

有一点须要注意,若是咱们的应用程序给系统发送的本地通知是周期性的,那么即便把程序删了重装,以前的本地通知在重装时依然存在(没有从系统中移除)。app

所以咱们须要取消通知的方法,固然该对象也会在scheduledLocalNotifications数组中移除。ide

2.移除推送通知测试

① 暴力方法:直接取消全部推送spa

1 [[UIApplication sharedApplication] cancelAllLocalNotifications];

 ② 移除个别推送通知:code

 1     //获取全部的本地推送
 2     NSArray *allLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
 3     //取消指定推送
 4     //1.遍历全部的本地推送
 5     for (UILocalNotification *notification in allLocalNotifications)
 6     {
 7         //2.找指定的推送
 8         if ([notification.userInfo[kNotificationKey] isEqualToString:@"notification1"])
 9         {
10             //3.取消推送
11             [[UIApplication sharedApplication] cancelLocalNotification:notification];
12         }
13     }

 2、远程推送orm

① 概念:远程推送是在iOS 3.0之后被引入的功能,是当程序没有启动或不在前台运行时,告诉用户有新消息的一种途径,是从外部服务器发送到应用程序上的。通常说来,当要显示消息或下载数据的时候,通知是由远程服务器(程序的提供者)发送,而后经过苹果的推送通知服务APNS(Apple Push Notification Service)推送到设备的程序上。对象

做为提供者为程序开发和部署推送通知,必须经过iOS Developer Program Portal得到SSL证书。每一个证书限用于一个程序,使用程序的bundle ID做为标识。

证书有两种用途的:一种是针对sandbox(用于开发和测试),另一种针对发布产品。这两种运行环境拥有为各自指定的IP地址而且须要不一样的证书。还必须为两种不一样的环境获取各自的provisioning profiles。

② APNS:

1.APNS提供了两项基本的服务:

a.消息推送 --- 使用流式TCP套接字将推送通知做为二进制数据发送给APNs。消息推送有分别针对开发和测试用的sandbox、发布产品的两个接口,每一个都有各自的地址和端口。无论用哪一个接口,都须要经过TLS或SSL,使用SSL证书来创建一个安全的信道。提供者编制通知信息,而后经过这个信道将其发送给APNs。

b.反馈服务 --- 能够获得针对某个程序的发送失败记录。提供者应该使用反馈服务周期性检查哪些设备一直收不到通知,不须要重复发送通知到这些设备,下降推送服务器的负担。

2.APNS的工做机制:

a.应用程序注册远程消息推送

 1     if ([UIDevice version] >= 8.0)
 2     {
 3         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
 4         //设置类型
 5         [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 6         //注册远程推送
 7         [[UIApplication sharedApplication] registerForRemoteNotifications];
 8     }
 9     else
10     {
11         //iOS8如下必须设置类型
12         [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
13          UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
14     }

 

b.应用程序向APNS获取DeviceToken

c.APNS接收后返回DeviceToken

 1 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
 2 {
 3     NSLog(@"%@",deviceToken);
 4     //提示:提交苹果服务器须要去除<>和空格 
 5     NSString *string = [NSString stringWithFormat:@"%@",deviceToken];
 6     string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
 7     string = [string stringByReplacingOccurrencesOfString:@">" withString:@""];
 8     string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
 9     NSLog(@"%@",string);
10 }

 

d.应用程序将DeviceToken发送给PUSH服务端程序(Provider)

e.Provider将DeviceToken和推送内容上传至APNS

f.APNS将推送内容推送至全部注册过的iPhone上

相关文章
相关标签/搜索