Swift3.0集成极光推送
现在很多程序都开始使用Swift开发了,但是第三方库大多数都是用OC写的,所以我们要使用Swift和OC混编。今天的内容主要讲Swift3.0集成极光推送。
1.准备工作
集成指南,极光上说的都很清楚,把创建应用和配置工程实现。SDK下载地址。在桥接头文件中添加
#import "JPUSHService.h" // iOS10注册APNs所需头文件 #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
2.Swift3.0集成
(1)AppDelegate.swift中添加代理
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate,JPUSHRegisterDelegate { }
(2)注册推送及处理应用未打开时收到推送消息
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 通知注册实体类 let entity = JPUSHRegisterEntity(); entity.types = Int(JPAuthorizationOptions.alert.rawValue) | Int(JPAuthorizationOptions.sound.rawValue) | Int(JPAuthorizationOptions.badge.rawValue); JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self); // 注册极光推送 JPUSHService.setup(withOption: launchOptions, appKey: "845b93e08c7fa192df019c07", channel:"Publish channel" , apsForProduction: false); // 获取推送消息 let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>; // 如果remote不为空,就代表应用在未打开的时候收到了推送消息 if remote != nil { // 收到推送消息实现的方法 self.perform(#selector(receivePush), with: remote, afterDelay: 1.0); } return true; }
(3)实现代理方法
// MARK: -JPUSHRegisterDelegate // iOS 10.x 需要 @available(iOS 10.0, *) func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) { let userInfo = notification.request.content.userInfo; if notification.request.trigger is UNPushNotificationTrigger { JPUSHService.handleRemoteNotification(userInfo); } completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue)) } @available(iOS 10.0, *) func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) { let userInfo = response.notification.request.content.userInfo; if response.notification.request.trigger is UNPushNotificationTrigger { JPUSHService.handleRemoteNotification(userInfo); } completionHandler(); // 应用打开的时候收到推送消息 NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo) } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { JPUSHService.handleRemoteNotification(userInfo); completionHandler(UIBackgroundFetchResult.newData); } // 接收到推送实现的方法 func receivePush(_ userInfo : Dictionary<String,Any>) { // 角标变0 UIApplication.shared.applicationIconBadgeNumber = 0; // 剩下的根据需要自定义 self.tabBarVC?.selectedIndex = 0; NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo) } }
我看网上没有人写用Swift3.0集成极光推送,自己集成的时候遇到了很多坑,所以分享出来,希望大家可以少浪费点大脑细胞☺。
相关推荐
发条戏子 2019-12-17
fengjb 2019-11-05
缘来如此 2015-04-01
jscjxysx 2019-09-08
liujia 2019-07-01
wangdazhu0 2015-10-06
好好学习天天 2019-06-27
MonkeyKingBi 2019-06-27
手把手教你学安卓 2019-06-21
mlsnatalie 2015-04-01
QiHY 2014-05-07
IT运维小白 2016-04-15
KUNPHP 2018-09-06
张明云的知识共享 2018-05-21
安在信息安全新媒体 2018-04-04
龙玉崇拜的起源与华夏北来说 2018-01-25
稀土 2017-12-27
chenxiangpeng 2019-06-28
大数据实战派 2019-06-21