日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 集成极光推送 (swift版)

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 集成极光推送 (swift版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里寫自定義目錄標題

  • iOS 集成極光推送 (swift版)
    • 1、使用cocoapods安裝極光推送SDK
    • 2、注冊及配置極光推送相關
    • 3、使用別名做唯一標識推送到指定用戶的設備
        • 搞定!!!

iOS 集成極光推送 (swift版)

1、使用cocoapods安裝極光推送SDK

之前項目使用的是信鴿推送,但是后面不知怎么地,就是收不到推送消息了,安卓,iOS都是,然后就換成了極光;
我用的是cocoapods安裝,也可以在官網下載SDK拖拽進你的工程里
cocoapods安裝及使用[https://www.jianshu.com/p/265d8e4dd73a]

在Podfile添加JPush

source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' target '******' do pod 'JPush' #極光推送 use_frameworks! end

然后在終端進入你的項目目錄,執行 pod install

2、注冊及配置極光推送相關

在Header.h引入相關文件

#ifndef Header_h #define Header_h /*極光推送*/ #import "JPUSHService.h" // iOS10注冊APNs所需頭文件 //#ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif #endif /* Header_h */

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate,JPUSHRegisterDelegate {func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {//注冊極光推送registerJPush()return true}func registerJPush(){//注冊極光推送let entity = JPUSHRegisterEntity()entity.types = 1 << 0 | 1 << 1 | 1 << 2JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)//需要IDFA 功能,定向投放廣告功能//let advertisingId = ASIdentifierManager.shared().advertisingIdentifier.uuidString//通知類型(這里將聲音、消息、提醒角標都給加上)let userSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound],categories: nil)if ((UIDevice.current.systemVersion as NSString).floatValue >= 8.0) {//可以添加自定義categoriesJPUSHService.register(forRemoteNotificationTypes: userSettings.types.rawValue,categories: nil)}else {//categories 必須為nilJPUSHService.register(forRemoteNotificationTypes: userSettings.types.rawValue,categories: nil)}//監聽自定義消息的接收//let defaultCenter = NotificationCenter.default//defaultCenter.addObserver(self, selector: #selector(networkDidReceiveMessage(notification:)),// name:Notification.Name.jpfNetworkDidReceiveMessage, object: nil)JPUSHService.setup(withOption: launchOptions, appKey: "appKey", channel: "App Store", apsForProduction: false, advertisingIdentifier: nil)}//極光推送需要實現的代理方法// 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.userInfoif notification.request.trigger is UNPushNotificationTrigger {JPUSHService.handleRemoteNotification(userInfo)}// 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置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.userInfoif response.notification.request.trigger is UNPushNotificationTrigger {JPUSHService.handleRemoteNotification(userInfo)}// 系統要求執行這個方法completionHandler()}//點推送進來執行這個方法func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {JPUSHService.handleRemoteNotification(userInfo)completionHandler(UIBackgroundFetchResult.newData)}//系統獲取Tokenfunc application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {UserDefaults.standard.set(deviceToken, forKey: "deviceToken")JPUSHService.registerDeviceToken(deviceToken)}//獲取token 失敗func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { //可選print("did Fail To Register For Remote Notifications With Error: \(error)")}@available(iOS 10.0, *)func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification?) {}func applicationDidEnterBackground(_ application: UIApplication) {//在應用進入后臺時清除推送消息角標application.applicationIconBadgeNumber = 0JPUSHService.setBadge(0)} }

appKey在極光推送開發者服務平臺里找,注冊一個應用的時候會自動生成

3、使用別名做唯一標識推送到指定用戶的設備

我這里使用的是登陸成功后接口返回的用戶手機號做別名,seq是序列號,隨便填,我這里用的是用戶id

JPUSHService.setAlias("user\(loginInfo.telephone!)", completion: { (iResCode, iAlias, seq) inprint("iResCode---\(iResCode)")print("iAlias---\(iAlias ?? "")")print("seq---\(seq)")}, seq: loginInfo.id!)

用戶退出登錄后移除別名,避免用戶使用其他設備登錄后當前設備依然還能收到推送消息

JPUSHService.deleteAlias({ (iResCode, iAlias, seq) inprint("iResCode---\(iResCode)")print("iAlias---\(iAlias ?? "")")print("seq---\(seq)")}, seq: Int(UserDefaults.standard.string(forKey: "userId")))

搞定!!!

總結

以上是生活随笔為你收集整理的iOS 集成极光推送 (swift版)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。