好久没写文章了巩固一下技能点
推送第三方平台:极光,融云等等
案列一般当应用激活并且在前台时也就是用户正在使用是接收不到通知提示的
这时候如果有这个需求我们可以使用远程加本地的方式来开发
这里以极光为例子
申请push证书
1)生成.csr证书
打开钥匙串工具(Finder -> 应用程序 -> 实用工具 -> 钥匙串访问),
打开后点击顶部菜单栏““钥匙串访问”出现如下界面,
选择 证书助理 -> 从证书颁发机构请求证书
这个证书是证明你电脑被该证书颁发机构承认了
该证书可以共用一次制作以后都能用
2)appid
https://developer.apple.com的
Certificates, Identifiers & Profiles
选中appids选项左上角添加
填写appid的名字
和bundle id
勾选push notifications
生成以后选中编辑将csr证书上传
最后生成一个.cer文本
3)p12证书制作
以前需要在终端转换一下现在不需要了只要在双击.cer证书导入钥匙串然后选中右击导出
需要输入密码然后生成P12证书
证书弄好了
pod 'JPush'
Pod install
导入头文件
import <JPUSHService.h>
ifdef NSFoundationVersionNumber_iOS_9_x_Max
import <UserNotifications/UserNotifications.h>
endif
<JPUSHRegisterDelegate>
8.0需要在Capabillities中打开push notifications
//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
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];
[JPUSHService setupWithOption:launchOptions appKey:@"fd0b0881067ad58f0b1759e3"
channel:@"APP stroe"
apsForProduction:1
advertisingIdentifier:nil];
回调
//注册apns成功上报devicetoken
-
(void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
//失败 (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
// 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三种类型可以选择设置
设置当应用处于前台使用中时也能接收通知
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
/// iOS10处理远程推送
[JPUSHService handleRemoteNotification:userInfo];
换掉默认提示音我们可以根据字典中有一个属性值来换声音
// pushDic = userInfo;
/// 应用处于前台收到推送的时候转成本地通知 ===========================
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDictionary *dic = userInfo[@"aps"];
notification.alertTitle =dic[@"alert"];
// notification.alertBody =@"aaaaaa";
notification.userInfo = userInfo;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}else{
// NSLog(@"++++++++++++++++++++++++++");
/// 应用处于前台收到本地通知不会出现在通知中心 借用极光推送的方法将本地通知添加到通知栏 ==============================
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
}
// 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 iOS6
[JPUSHService handleRemoteNotification:userInfo];
}