iPush 1.2.1

iPush 1.2.1

adam维护。



iPush 1.2.1

  • Adam.Hu

iPush

CI Status Version License Platform

iPush同时提供APNS和Tcp两种方式的下行推送功能,用户可以根据项目需要,选择性使用。 如果只是简单接入APNS代理推送功能,说明4中关于Tcp功能接入部分可以忽略,以下是进行接入的流程步骤

一:开设账户,生成appkey

联系工作人员,提供 boundId,测试环境apns证书,正式环境apns证书,生成 appkey。

二:集成SDK

1.CocoaPods 集成

iPush支持CocoaPods方式和手动集成两种方式。我们推荐使用CocoaPods方式集成,以便随时更新至最新版本。

在Podfile中添加以下内容。

 pod 'iPush'

执行以下命令,安装iPush。

 pod install

如果无法安装SDK最新版本,执行以下命令更新本地的CocoaPods存储库列表。

 pod repo update

2.主项目添加 Push Notifications 功能

Targets中选择主项,点击右侧的 Signing & Capabilities, 点击 + ,选择 Push Notifications选项。

三:代码流程接入

1. 在 AppDelegate.m 文件中引入 iPush,并初始化(以 Swift 项目为例)。

import iPush

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    //选择运行环境 
    //1:测试环境,0:正式环境(默认)
    PushApi.getInstance().DEBUG = 1    
    //初始化sdk
    PushApi.getInstance().initSdk(appKey:“你申请生成的appkey”)  
    //申请获取 deviceToken
    PushApi.getInstance().registerNotification(application: application, delegate: self)    
    //生成register_id
    PushApi.getInstance().registerUID { (response:PHResponse<String>) in                    
        print("*** [SDK] \(response.data ?? "")")
    }
    return true
}

申请获取 deviceToken

调用 PushApi.getInstance().registerNotification(application: application, delegate: self) 方法用于申请获取 deviceToken 值,成功后会通过以下 UNUserNotificationCenterDelegate 代理方法回调(原生 deviceToken 的回调代理方法)

//MARK:- deviceToken申请结果回调
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
}

生成 iPush 平台下的唯一 id (registerId)

registerId 用于在 iPush 平台唯一标识用户设备。生成唯一 id 后,上传到自己的服务中,与自己的业务系统用户 id 进行绑定。当需要进行推送时,业务系统附带 registerId,调用 iPush 后台接口,对指定用户的特定设置进行精准推送。

生成 registerId 的代码如下,可以在启动的回调方法中进行调用:

PushApi.getInstance().registerUID { (response:PHResponse<String>) in
    if response.isSuccess = true, let sId:String = response.data{
        print("Register_Id = \(sId)")
    }
}

2. 提交 APNS 返回的推送 token (deviceToken)

获取到 deviceToken 后,调用 pushToken 方法,提交 pushToken 到推送平台,并确保提交成功。

PushApi.getInstance().pushToken(data:deviceToken) { (response:PHResponse<Bool>) in
    if response.isSuccess == true{
        print("提交成功")
    }
}

3. 实现推送回调方法

当收到 apns 推送时,将回调 UNUserNotificationCenterDelegate 代理方法参考如下(原生 apns 消息回调代理方法)。如要支持 iOS 10 以下的系统,请自行补充版本兼容代码。

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
    
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void){
    
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
}

4. 设置 Tcp 推送监听器

当需要使用 Tcp 通道进行推送时,需要接入 iPush Tcp 推送流程,接入顺序如下:

  1. 实现 PushCmdDelegate 代理方法,当收到 tcp 数据时自动回调 onReceiveCmd 方法
public protocol PushCmdDelegate: NSObject {
    func onReceiveCmd(item:PHCMDItem)
}

PHCMDItem 封装了指令中的所有可用信息,数据结构如下:

   public var type:Int?
   public var data:Any?
   public var title:String?
   public var content:String?
  1. 设置Tcp推送监听器

通过上述步骤,完成实例化监听器后,通过以下代码实现监听器的绑定

PushApi.getInstance().delegate = self

四. 运行Demo注意事项

1. pod install

下载demo项目后,打开主目录,在终端中执行 pod install。

2. 替换你的 appkey

打开项目,打开 AppDelegate文件,在 initSDK 方法中替换成你申请成功的appkey

注意: 测试环境下的appkey,与正式环境下的appkey不相同,请分开申请。

License

The MIT License (MIT)

Copyright (c) 2020 Adam

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.