极光征文程序员@IT·互联网

极光正文 | 那夜,极光带我走进极光推送

2018-12-27  本文已影响58人  行走的菜谱

外面电闪雷鸣,看了下时间,已经到11点了,办公室内一片繁忙,没有嘈杂的聊天声音,只有“啪啪啪”的键盘声音,此时此刻我正在疯狂的调试着bug,心里不停的在吐槽,这手机推送到底要怎么搞,再弄不出来,老板就要炒我鱿鱼了,想到这里,心里一阵委屈,我这么拼,之前疯狂码代码的努力难道要白费了吗,难道要死在这个推送上吗!!!
突然浏览器的右下角弹出了一个广告:极光推送,解放你的双手!万念俱灰的我抱着一丝侥幸心理点开了广告,跳转到了一个官网,但是没想到这个广告竟然改变了我的命运!

极光

PS:此时此刻我的心理乐开了花,充满期待又充满惊恐,怕这个是骗人的

试一试吧,于是我查看文档,此时此刻我的疑惑已经烟消云散,xxx为啥极光的广告这么少,害的我差点准备跑路,极光官网

一、注册

二、点击极光推送的文档

三、集成步骤

集成步骤

1. 这个时候心里已经吃了一颗定心丸了,不怕被炒鱿鱼了,在导入之前需要配置几个证书,这个对于帅气的我来说不在话下,三下两下就配好了,在这里具体的步骤可以参考极光推送证书配置指南,这里写的比我详细多了,感谢极光工作人员的认真细致,不然我又要花大把的时间在配置证书上,么么哒😘

PS:此时此刻已经11.30了,不过我不怕,有极光在,心里很安心

2. 导入步骤,这个时候心里越来越信任极光了

pod 'JPush', '3.1.0'
  1. 在极光官网下载最新 SDK
  2. 将 SDK 包解压,在 Xcode 中选择 “Add files to 'Your project name'...”,将解压后的 lib 子文件夹(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目录中。
  3. 添加 Framework

注意: 如果集成 JPush 3.0.1 及以上版本, 且同时集成极光其他 SDK(如:JMessage 3.0.0 及以上版本)

  1. Cocoapods 导入,建议都更新为线上最新版本,来避免 JCore 版本不一致导致的冲突
  2. 手动导入,在工程中只需保留一个最新版本的 jcore-ios-x.x.x.a 静态库文件。
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
//初始化APNs 
 JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
 entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
 [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
 //初始化JPush 
 #ifdef DEBUG
 [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:NO];
 #else
 [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:YES];
 #endif
@interface AppDelegate ()<JPUSHRegisterDelegate>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}
@end
#pragma mark- JPUSHRegisterDelegate

// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
  if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //从通知界面直接进入应用
  }else{
    //从通知设置界面进入应用
  }
}

// iOS 10 Support
- (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 三种类型可以选择设置
}

// iOS 10 Support
- (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, For systems with less than or equal to iOS 6
  [JPUSHService handleRemoteNotification:userInfo];
}
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        XBLog(@"------>registrationID: %@",[JPUSHService registrationID]);
        //上传极光registrationID给服务器
 }];

PS:已经12点了,这个时候万事俱备只欠东风,来cmd+R,心里的侥幸心理又出现了,假如不行怎么办,别怕,极光给出了方案

常见收不到通知的原因

此时此刻见证奇迹的时刻到了,为了验证集成到底有没有正确,请看下边

测试

点击发送,过了1秒钟,可能更短,(时间比我想象中的短,极光果然是真男人,就是快)手机收到通知,心里的石头落到井里了

PS:此时此刻,我闭上了眼睛,心里默默的回忆着整个过程(省略1w以上的字)

过了几天我通过微信加上了极光的客服,还有市场,还有技术,发现他们是一群可爱的,美丽的,善良的小姐姐,我心里默默的发誓,我要给极光生猴子!!!

附上极光技术客服解答群:445928015,里面的小姐姐很有耐心,可以找她谈心,谈技术,谈理想都行

最后,感谢极光!!!😘😘😘

本文为极光征文参赛文章

上一篇 下一篇

猜你喜欢

热点阅读