你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift3.0集成極光推送

Swift3.0集成極光推送

編輯:IOS開發綜合

本文將為大家闡明Swift3.0集成極光推送的相關引見,詳細實例請看下文

  如今很多順序都開端運用Swift開發了,但是第三方庫大少數都是用OC寫的,所以我們要運用Swift和OC混編。明天的內容次要講Swift3.0集成極光推送。

1.預備任務

  集成指南,極光上說的都很清楚,把創立使用和配置工程完成。SDK下載地址。在橋接頭文件中添加

#import "JPUSHService.h"
// IOS10注冊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)注冊推送及處置使用未翻開時收到推送音訊

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        // 告訴注冊實體類
        let entity = JPUSHRegisterEntity();
        entity.types = Int(JPAuthorizationOptions.alert.rawValue) |  Int(JPAuthorizationOptions.sound.rawValue) |  Int(JPAuthorizationOptions.badge.rawValue);
        JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self);
        // 注冊極光推送
        JPUSHService.setup(withOption: launchOptions, appKey: "845b93e08c7fa192df019c07", channel:"Publish channel" , apsForProduction: false);
        // 獲取推送音訊
        let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>;
        // 假如remote不為空,就代表使用在未翻開的時分收到了推送音訊
        if remote != nil {
            // 收到推送音訊完成的辦法
            self.perform(#selector(receivePush), with: remote, afterDelay: 1.0);
        }
        
        return true;
    }

(3)完成代理辦法

 // 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();
        // 使用翻開的時分收到推送音訊
        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);
    }
    // 接納到推送完成的辦法
    func receivePush(_ userInfo : Dictionary<String,Any>) {
        // 角標變0
        UIApplication.shared.applicationIconBadgeNumber = 0;
        // 剩下的依據需求自定義
        self.tabBarVC?.selectedIndex = 0;
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo)
    }
}

  我看網上沒有人寫用Swift3.0集成極光推送,自己集成的時分遇到了很多坑,所以分享出來,希望大家可以少糜費點大腦細胞☺。 

謝謝您的訪問.

【Swift3.0集成極光推送】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved