Swift 集成友盟推送

1、前期准备

集成之前, 请在http://push.umeng.com/申请开通【友盟+】消息推送服务。
下载 UMessage_Sdk_All_x.x.x.zip并解压缩

2、配置

2.1.1 引入库文件

增加UserNotifications.framework到项目中。
具体操作如下:点击项目---->TARGET---->Build Phases---->Link Binary with Libraries ---->左侧+号---->搜索UserNotifications---->选中UserNotifications.framework---->点击Add

2.1.2 配置(可选)

如果您使用了-all_load,可能需要添加libz的库:
TARGETS-->Build Phases-->Link Binary With Libraries--> + -->libz.dylib

2.1.3 打开推送开关

点击项目---->TARGET
---->Capabilities
,将这里的Push Notification

的开关打开,效果如图所示:
Swift 集成友盟推送_第1张图片
这里写图片描述

注意:一定要打开Push Notification,且两个steps都是正确的,否则会报如下错误:Code=3000 "未找到应用程序的“aps-environment”的授权字符串"

3、开始集成

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //推送
        UMessage.start(withAppkey: UMAppKey, launchOptions: launchOptions, httpsEnable: true)
        UMessage.registerForRemoteNotifications()
        
        //iOS10必须添加下面这段代码
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current
            center().delegate = self 
            center().requestAuthorization(options:[.badge,.alert,.sound] , completionHandler: { (granted, error) in
                if granted {
                    //点击允许
                    //这里可以添加一些自己的逻辑
                }else{
                    //点击不允许
                    //这里可以添加一些自己的逻辑
                }
            })
            
        } else {
            // Fallback on earlier versions
        }
        //打开日志,方便调试
        UMessage.setLogEnabled(true)
        return true
    }

 /**
   UNUserNotificationCenterDelegate
 */
 //iOS10以下使用这个方法接收通知
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        UMessage.didReceiveRemoteNotification(userInfo)
        
    }
   
    @available(iOS 10.0, *)
    //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))! {
            let info = userInfo as NSDictionary
            print(info)
            //应用处于前台时的远程推送接受
            UMessage.setAutoAlert(false)
            UMessage.didReceiveRemoteNotification(userInfo)
        }else{
            //应用处于前台时的远程推送接受
        }
        completionHandler([.alert,.sound,.badge])
    }
    
    @available(iOS 10.0, *)
    //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))! {
            let info = userInfo as NSDictionary
            print(info)
            //应用处于后台时的远程推送接受
            UMessage.didReceiveRemoteNotification(userInfo)
        }else{
            //应用处于前台时的远程推送接受
        }
    }

你可能感兴趣的:(Swift 集成友盟推送)