Swift3.0集成極光推送

??現(xiàn)在很多程序都開始使用Swift開發(fā)了,但是第三方庫(kù)大多數(shù)都是用OC寫的,所以我們要使用Swift和OC混編。今天的內(nèi)容主要講Swift3.0集成極光推送。

1.準(zhǔn)備工作

集成指南,極光上說(shuō)的都很清楚,把創(chuàng)建應(yīng)用和配置工程實(shí)現(xiàn)。SDK下載地址。在橋接頭文件中添加

#import "JPUSHService.h"
// iOS10注冊(cè)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)注冊(cè)推送及處理應(yīng)用未打開時(shí)收到推送消息

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        // 通知注冊(cè)實(shí)體類
        let entity = JPUSHRegisterEntity();
        entity.types = Int(JPAuthorizationOptions.alert.rawValue) |  Int(JPAuthorizationOptions.sound.rawValue) |  Int(JPAuthorizationOptions.badge.rawValue);
        JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self);
        // 注冊(cè)極光推送
        JPUSHService.setup(withOption: launchOptions, appKey: "845b93e08c7fa192df019c07", channel:"Publish channel" , apsForProduction: false);
        // 獲取推送消息
        let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>;
        // 如果remote不為空,就代表應(yīng)用在未打開的時(shí)候收到了推送消息
        if remote != nil {
            // 收到推送消息實(shí)現(xiàn)的方法
            self.perform(#selector(receivePush), with: remote, afterDelay: 1.0);
        }
        
        return true;
    }

(3)實(shí)現(xiàn)代理方法

 // 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();
        // 應(yīng)用打開的時(shí)候收到推送消息
        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);
    }
    // 接收到推送實(shí)現(xiàn)的方法
    func receivePush(_ userInfo : Dictionary<String,Any>) {
        // 角標(biāo)變0
        UIApplication.shared.applicationIconBadgeNumber = 0;
        // 剩下的根據(jù)需要自定義
        self.tabBarVC?.selectedIndex = 0;
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo)
    }
}

我看網(wǎng)上沒(méi)有人寫用Swift3.0集成極光推送,自己集成的時(shí)候遇到了很多坑,所以分享出來(lái),希望大家可以少浪費(fèi)點(diǎn)大腦細(xì)胞?。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 因?yàn)橐Y(jié)局swift3.0中引用snapKit的問(wèn)題,看到一篇介紹Xcode8,swift3變化的文章,覺(jué)得很詳細(xì)...
    uniapp閱讀 4,872評(píng)論 0 12
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,327評(píng)論 4 61
  • 小程序wxml文件中增加授權(quán)標(biāo)簽 在小程序js文件中增加getPhoneNumber方法 在后臺(tái)增加接口 user...
    Monster_鼎輝閱讀 16,344評(píng)論 2 10
  • 湖光草色相映翠,習(xí)習(xí)清風(fēng)游人醉。 柳蔭如傘遮雁巢,雙雁輪臥愛(ài)相隨。 母語(yǔ)呢喃知美景,急盼破殼...
    冰川映彩虹閱讀 242評(píng)論 0 0
  • 昨天晚上,我們來(lái)到了洛陽(yáng)的木扎嶺,聽(tīng)說(shuō)那里晚上很冷,需要拿厚衣服,沒(méi)來(lái)之前,我也是滿心的期待呢! 我...
    依卓1閱讀 390評(píng)論 0 1

友情鏈接更多精彩內(nèi)容