概述
相关配置完成后,我们就来实现相应的远程推送功能。
一、向APNS服务器注册
大致流程:
1. 一般在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registerForRemoteNotificationWithOptions:launchOptions];
return YES;
}
中注册远程推送
- (void)registerForRemoteNotificationWithOptions:(NSDictionary *)launchOptions {
// apn 内容获取:
// 一般启动 lauchOptions 为 nil
// 远程启动 lauchOptions 有值
/**
如果launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用户点击apn 通知导致app被启动运行;
如果不含有对应键值则表示 App 不是因点击apn而被启动,可能为直接点击icon被启动或其他。
*/
// NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
/**
//系统原生的注册方法
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[application registerUserNotificationSettings:setting];
}else{
UIRemoteNotificationType *type = UIRemoteNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];
}
*/
//Required
/**
JPush 注册方法
*/
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
/**
远程推送侧滑所显示的button “OK”,“回复“,
根据远程推送接收到的数据(JSON)中key : ”category“
来判断用哪个 action
*/
NSMutableSet *categories = [NSMutableSet set];
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"identifier";
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"test2";
action.title = @"OK";
action.activationMode = UIUserNotificationActivationModeBackground;
action.authenticationRequired = YES;
action.destructive = NO;//YES显示为红色,NO显示为蓝色
NSArray *actions = @[ action ];
[category setActions:actions forContext:UIUserNotificationActionContextMinimal];
[categories addObject:category];
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:categories];<p class="p1"><span class="s1">//categories </span><span class="s2">可以传</span><span class="s1"> nil</span></p>
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//Required
// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchOptions appKey:@"c3cecb70fdd6461eb0746347"
channel:@"Publish channel"
apsForProduction:false
advertisingIdentifier:nil];
}
UIMutableUserNotificationAction 点击回调函数
// Called when your app has been activated by the user selecting an action from
// a remote notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler {
}
服务端设置
服务端payload格式:aps增加category字段,当该字段与客户端UIMutableUserNotificationCategory的identifier匹配时,触发设定的action和button显示。
//payload example:
{
"aps":{
"alert":"example",
"sound":"default",
"badge": 1,
"category":"identifier"
}
}
2. APNS 服务器放回DeviceToken,如果注册成功,上传deviceToken到自己的服务器(或者第三方服务器,如JPush)。
2.1注册成功
// 向APNS 注册成功后放回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *string = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"deviceTokenString : %@ " , string);
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
2.1 注册失败
// 注册远程推送失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
二、接收/处理 远程推送
// 基于iOS 6 及以下的系统版本,如果 App状态为正在前台或者点击通知栏的通知消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
/**
* iOS7之后,接收到推送消息的代理方法
* 接收到推送通知后,需要判断当前程序所处的状态,并根据APP的业务做出相应的处理
* 在处理完后,需要调用completionHandler block 回调
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"%@", NSLocalizedString(@"TestA", nil));
NSLog(@"this is iOS7 Remote Notification");
if (application.applicationState == UIApplicationStateInactive){
//程序处于非活跃状态,手机锁屏,双击home键等
NSLog(@"inactive");
}else if (application.applicationState == UIApplicationStateBackground){
//应用程序处于后台,例如,点击home键进入后台等
NSLog(@"background");
}else if (application.applicationState == UIApplicationStateActive){
//应用程序活跃状态,应用程序在前台运行时
NSLog(@"action");
}
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
// 执行completionHandler有两个目的
//
// 1> 系统会估量App消耗的电量,并根据传递的UIBackgroundFetchResult 参数记录新数据是否可用
//
// 2> 调用完成的处理代码时,应用的界面缩略图会自动更新
//
// 注意:接收到远程通知到执行完网络请求之间的时间不能超过30秒
}
最后
以上就是淡然抽屉为你收集整理的iOS 远程推送 总结的全部内容,希望文章能够帮你解决iOS 远程推送 总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复