Swift 集成友盟推送

1、前期准备

集成之前, 请在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 的开关打开,效果如图所示:

注意:一定要打开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{
            //应用处于前台时的远程推送接受
        }
    }

复制代码

转载于:https://juejin.im/post/5a31ebdaf265da431876c22c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值