APNS:
就是APP的服务端在用户未打开APP进程时,还能发通知给用户APP的服务技术
相关概念
Provider:提供推送的第三方服务系统
Device:苹果设备,例如iphone和ipad等
APNS:苹果推送消息服务,属于苹果的服务
APP:安装在苹果设备上的应用程序
DeviceToken:设备的标识,用于确定接收通知的设备及APP
Payload:推送消息的传输形式
推送使用:
1.需要去极光官网申请一个appkey,类似:478c42dw2e58e01c4a9b449a
2.配置极光推送,可以设置声音,震动,alert,不设置后面是不可以用这些功能的,启动SDK
代码:
[JPUSHService setupWithOption:launchOptions
appKey:appKey
channel:channel
apsForProduction:isProduction];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//监听
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kJPFNetworkDidSetupNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kJPFNetworkDidCloseNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kJPFNetworkDidRegisterNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
[defaultCenter addObserver:self selector:@selector(serviceError:) name:kJPFServiceErrorNotification object:nil];
2.注册设备,每次启动程序都要注册,可以通过前面的监听,判断是否建立连接,是否注册成功
[[QYPushHelper shared] setTags:
[NSSet setWithObjects:
[NSString stringWithFormat:@"test_company_%@",account.companyId],
[NSString stringWithFormat:@"test_company_ios_%@",account.companyId],
[NSString stringWithFormat:@"test_ios_user_%@",account.userId],
[NSString stringWithFormat:@"test_user_ios_%@",account.userId],
[NSString stringWithFormat:@"test_%@",account.phone],
[NSString stringWithFormat:@"test_%@",account.userId],
[NSString stringWithFormat:@"test_user_phone_%@",account.phone],
[NSString stringWithFormat:@"test_notice_user_%@",account.userId],
[NSString stringWithFormat:@"test_ios_user_%@",account.userId],nil]
alias:[NSString stringWithFormat:@"i%@",account.userId] success:^
{
}failure:^(int code, NSString *log)
{
}];
3.接收消息
/**
* APNS消息 程序处于后台
*
* @param notification
*/
- (void)handleRemoteNotification:(NSDictionary *)remoteInfo
{
// Required
[JPUSHService handleRemoteNotification:remoteInfo];
QYPushResult *pushResult = [[QYPushResult alloc] init];
pushResult.pushType = QYPushTypeAPNS;
pushResult.msgid = [remoteInfo[@"_j_msgid"] longLongValue];
pushResult.content = remoteInfo[@"aps"][@"alert"];
NSMutableDictionary *extras = [[NSMutableDictionary alloc] init];
[remoteInfo enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
if (![key isEqualToString:@"aps"]&&![key isEqualToString:@"_j_msgid"])
{
[extras setObject:obj forKey:key];
}
}];
pushResult.extras = extras;
pushResult.badge = [remoteInfo[@"aps"][@"badge"] integerValue];
pushResult.sound = remoteInfo[@"aps"][@"sound"];
//自己处理
[self didReceiveRemoteNotification:pushResult];
}
/**
* 非APNS消息 ,程序处于前台
*
* @param notification
*/
- (void)networkDidReceiveMessage:(NSNotification *)notification
{
NSDictionary * userInfo = [notification userInfo];
QYPushResult *pushResult = [[QYPushResult alloc] init];
pushResult.pushType = QYPushTypeMessage;
pushResult.content = userInfo[@"content"];
pushResult.extras = userInfo[@"extras"];
[self didReceiveRemoteNotification:pushResult];
}
4.退出登录 清空tag'
//推送
[[QYPushHelper shared] setTags:[NSSet set] alias:@"" success:^{
} failure:^(int code, NSString *log) {
}];