推送作为一个产品必备功能,越来越得到所有人的重用,市面上做推送的第三方公司也有很多,像个推,百度,甚至友盟也出了推送功能。用什么不是我们能决定的,而是上级或产品决定的,所以我负责的项目用了极光推送,我便来整理一下使用过程和踩了的坑。
开整
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import <AdSupport/AdSupport.h>
<JPUSHRegisterDelegate>
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定义categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
/**
注册成功返回deviceToken 再将deviceToken注测到极光
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"\nAPNS注册成功 device token: %@",token);
/// 极光推送 - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
/**
远程推送注册失败
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"\nAPNS注册失败 原因: %@", error);
}
static NSString *appKey = @"你极光后台的appkey”;
static NSString *channel = @"App Store”; //指明应用程序包的下载渠道,为方便分渠道统计,具体值由你自行定义,如:App Store
static BOOL isProduction = FALSE; //证书环境,是否是生产环境证书,需要与运行时的Code Signing中配置的证书环境一致。
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系统要求执行这个方法
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required, iOS6或之前系统
[JPUSHService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
这个方法来处理当程序处于活跃状态时可以出现系统下落提示框,首先,人家在注释里说的很明白// Called when your app has been activated by the user selecting an action from a remote notification. 就是说这个方法会在你的app被用户点击远程通知中的某一个操作唤醒时被调用,也就是说,该方法首先不是来了通知就调用,也不是你点击通知栏将app置入前台时调用,而是,当你的通知中有多个操作,比如锁屏时左滑出现的查看按钮或者输入框,或者其他可操作区域时,进行相关操作后,app被唤醒时才会调用该方法。呵呵哒,没文化真可怕。我也被坑了。