首页 > 代码库 > 关于推送的一些记录要点
关于推送的一些记录要点
记录一下极光推送集成的一些食粮:
推送的要点无非:远程推送,本地推送,badge角标值的设定等。
1.badge
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];//本地badge值设置
[JPUSHServicesetBadge:0];//清空JPush服务器中存储的badge值
每次有推送的时候badge +1 这个操作需要服务器去处理。包括推送声音的设置也都是服务端那边在处理设置。
2.推送目标的设备选择方式
1)广播
2)tag(标签):
用标签来进行大规模的设备属性、用户属性的分群。
为安装应用程序的用户打上标签,以便于开发者根据标签来批量下发push消息。
3)alias(别名):
用别名来标识一个用户,一个设备只能绑定一个别名,但多个设备可以绑定同一个别名。
同一个应用程序内,对不同的用户建议取不同的别名,尽可能根据别名来唯一确定用户。
4)RegistrationID(设备标识)
集成了Jpush SDK的应用程序在第一次成功注册到JPush服务器时,JPush服务器会给客户端返回一个唯一的该设备标识。ios9系统,应用卸载重装,RegistationID会发生改变。
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(registerDeviceId)name:kJPFNetworkDidLoginNotificationobject:nil];//登录成功后
-(void)registerDeviceId
{
[JPUSHService registrationID];
NSLog(@"registrationID:%@",[JPUSHServiceregistrationID]);
//在登录成功对应的方法中设置标签及别名
注意:在退出登陆时,要去除对标签及别名的绑定
/**tags alias
*空字符串(@“”)表示取消之前的设置
*nil,此次调用不设置此值
*每次调用设置有效的别名,覆盖之前的设置
*/
NSString *alias = @"hello";
[JPUSHServicesetTags:nilalias:alias fetchCompletionHandle:^(int iResCode,NSSet*iTags, NSString *iAlias) {
NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);//对应的状态码返回为0,代表成功
}];
三、本地通知 (获取apns推送内容)
//1.对于极光推送来说,如果App状态为未运行,函数application:didFinishLaunchingWithOptions:将被调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//本地通知内容获取
NSDictionary *remoteNotification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];//通过判断这个是不是为空可以判断app是通过推送启动的还是通过点击图标启动的。
NSLog(@"若launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用户点击本地通知导致app被启动运行;若不包含则可能为直接点击icon被启动或其他");
}
//2. 如果App状态为正在前台或者点击通知栏的通知消息,苹果的回调函数将被调用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 取得 APNs 标准信息内容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
// 取得Extras字段内容
NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服务端中Extras字段,key是自己定义的
NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field =[%@]",content,(long)badge,sound,customizeField1);
//判断程序是否在前台运行
if (application.applicationState ==UIApplicationStateActive) {
//如果应用在前台,在这里执行
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"极光推送"message:content delegate:nilcancelButtonTitle:@"ok"otherButtonTitles:nil,nil];
[alertViewshow];
}
// iOS 7 Support Required,处理收到的APNS信息
//如果应用在后台,在这里执行
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
[JPUSHServicesetBadge:0];//清空JPush服务器中存储的badge值
[application setApplicationIconBadgeNumber:0];//小红点清0操作
}
四、获取自定义消息推送内容
/**前台运行时,可接收由JPush下发的自定义消息
*获取自定义消息(只有在前端运行的时候才能收到自定义消息的推送)
* kJPFNetworkDidReceiveMessageNotification// 收到消息(非APNS)
*/
NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
[defaultCenteraddObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];
- (void)networkDidReceiveMessage:(NSNotification *)notification {
/**
*参数描述:
content:获取推送的内容
extras:获取用户自定义参数
customizeField1:根据自定义key获取自定义的value
*/
NSDictionary * userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"];
NSDictionary *extras = [userInfo valueForKey:@"extras"];
NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; ///服务端传递的Extras附加字段,key是自己定义的
NSInteger badge = [[[userInfovalueForKey:@"aps"]valueForKey:@"badge"]integerValue];
NSLog(@"%jiaobao--ld",(long)badge);
NSLog(@"custuserInfo:%@",userInfo);
NSLog(@"custcontent:%@",content);
NSLog(@"custextras:%@",extras);
NSLog(@"customizeField1:%@",customizeField1);
NSLog(@"cust获取注册ID:%@", [JPUSHServiceregistrationID]);
}
极光推送的协议(ios9之上。之下的用上面的协议)
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#pragma mark- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
//推送将要显示的时候触发
NSLog(@"代理方法1");
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10 前台收到远程通知:%@", @"ios10");
}
else {
// 判断为本地通知
NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
[JPUSHService setBadge:0];//清空JPush服务器中存储的badge值
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//小红点清0操作
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
//通过点击推送启动app的时候会触发(前台运行的时候,后台还没有测试)
NSLog(@"代理方法2");
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10 收到远程通知:%@", @"ios10");
// [rootViewController addNotificationCount];
}
else {
// 判断为本地通知
NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
completionHandler(); // 系统要求执行这个方法
[JPUSHService setBadge:0];//清空JPush服务器中存储的badge值
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//小红点清0操作
}
#endif
------感谢bohezhang的分享
关于推送的一些记录要点