推送(PUSH)

时间:2023-03-09 18:03:34
推送(PUSH)

一、本地推送

1.建立本地推送

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

注意这个方法只有在程序启动之后才会执行,因此当程序处于后台时,该方法不会执行。

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

因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。

2.移除推送通知

① 暴力方法:直接取消所有推送

 [[UIApplication sharedApplication] cancelAllLocalNotifications];

② 移除个别推送通知:

     //获取所有的本地推送
NSArray *allLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
//取消指定推送
//1.遍历所有的本地推送
for (UILocalNotification *notification in allLocalNotifications)
{
//2.找指定的推送
if ([notification.userInfo[kNotificationKey] isEqualToString:@"notification1"])
{
//3.取消推送
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}

二、远程推送

① 概念:远程推送是在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.应用程序注册远程消息推送

     if ([UIDevice version] >= 8.0)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
//设置类型
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
//注册远程推送
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
//iOS8以下必须设置类型
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}

b.应用程序向APNS获取DeviceToken

c.APNS接收后返回DeviceToken

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@",deviceToken);
//提示:提交苹果服务器需要去除<>和空格
NSString *string = [NSString stringWithFormat:@"%@",deviceToken];
string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@">" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@",string);
}

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

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

f.APNS将推送内容推送至所有注册过的iPhone上