首先集成友盟可以到官方文档中集成, 配置相应的属性, 建立桥接文件, 主要导入一下文件
//友盟
#import <UMCommon/UMCommon.h>
#import <UMCommonLog/UMCommonLogHeaders.h> //日志
#import <UMPush/UMessage.h> //推送
APP启动方法中设置友盟配置代码如下
//开发者需要显式的调用此函数,日志系统才能工作
UMCommonLogManager.setUp()
//打开加密传输
UMConfigure.setEncryptEnabled(true)
//设置日志
UMConfigure.setLogEnabled(true)
//初始化
UMConfigure.initWithAppkey(um_appKey, channel: um_channel)
// 推送通知相关
let entity = UMessageRegisterEntity.init()
entity.types = Int(UMessageAuthorizationOptions.badge.rawValue | UMessageAuthorizationOptions.alert.rawValue | UMessageAuthorizationOptions.sound.rawValue)
UNUserNotificationCenter.current().delegate = self
UMessage.registerForRemoteNotifications(launchOptions: launchOptions, entity: entity) { granted, error in
}
UNUserNotificationCenterDelegate代理
extension AppDelegate:UNUserNotificationCenterDelegate {
//iOS10新增:处理前台收到通知的代理方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if ((notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self)) != nil) {
UMessage.setAutoAlert(false)
UMessage.didReceiveRemoteNotification(userInfo)
//写了completionHandler前台才能接收到通知
completionHandler([.alert, .sound, .badge])
}else {
//应用处于前台时的本地推送接受
}
}
//iOS10新增:处理后台点击通知的代理方法
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if ((response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self)) != nil) {
UMessage.setAutoAlert(false)
UMessage.didReceiveRemoteNotification(userInfo)
}else {
//应用处于前台时的本地推送接受
}
}
//测试用的deviceToken
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceId = deviceToken.map{ String(format: "%02.2hhx", $0) }.joined()
UMessage.registerDeviceToken(deviceToken)
print("deviceToken====\(deviceId)")
}
}
// 登录成功时设置推送别名
UMessage.addAlias("\(loginData.itemId)", type: "iOS") { (response, error) in
if response == nil {
print(error?.localizedDescription ?? "")
}else {
print("友盟:别名绑定成功")
}
}
代码可以直接粘贴到项目中
如果获得的deviceToken 无法在测试时绑定,则可能需要删除手机上的APP,重新运行一下(方法之一), 还是不行则是其它问题,自行百度