Swift 整合友盟推送

z小志發表於2018-01-23

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{
            //應用處於前臺時的遠端推送接受
        }
    }

複製程式碼

相關文章