现在很多程序都开始使用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集成极光推送,自己集成的时候遇到了很多坑,所以分享出来,希望大家可以少浪费点大脑细胞☺。
网友评论