当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
8 Star 9 Fork 1

魔人舅舅 / xinge
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

腾讯信鸽Golang SDK(非官方版本)

Go Report Card GoDoc

前言

部分代码来源于信鸽官方推荐的FrontMage
原本想使用FrontMage的库,但不太符合我懒人的习惯,于是自己动手改造一下,分享给大家!

考虑到国内连接GitHub缓慢,代码也同步放在我的码云

更新日志

2019年6月22日 08:46:04

修复按账号推送无效的bug

2019年6月20日 10:43:32

增加OptionAndroidIntent方法

用法

安装

Go modules

gitee.com/kmlixh/xinge v1.0.3

普通方式

$ go get gitee.com/kmlixh/xinge

或者

$ go get github.com/kmlixh/xinge

全局推送

package xinge

import (
	"testing"
	"time"
	"fmt"
	"gitee.com/kmlixh/xinge"
)

var authorAndroid = xinge.Authorization{AppID:"085f557303c8b", SecretKey:"046cf0c53a1bf6683bb22020a0ed8fec"}
var authoriOS = xinge.Authorization{AppID:"d5089ed7c3200", SecretKey:"d46a1b7d9d5327df90519d758cee8a1d"}
var xgClient =xinge.NewXingeClient(authorAndroid,authoriOS,xinge.NewHttpClient())

//测试推送一条面向全员的Notify消息
func main() {
	//以下两个配置信息都是真实的,但是并未配置正确的客户端,此处仅用来测试服务器的返回是否一致
	msg := xinge.NewPushAllNotifyPushMsg(xinge.PlatformAndroid, "测试的标题", "测试的内容"+time.Now().String())
	resp := xgClient.Push(msg)
	fmt.Println(resp)
}

类型解析:

IAuth   //推送鉴权的接口;
Authorization //实现了IAuth接口,提供鉴权的相关操作
XgClient  //信鸽推送的客户端,推送相关的操作都由XgClient完成
IPushMSg //消息抽象接口
PushMsg   //被推送的消息,抽象和封装了推送消息本身。
Platform //平台;字符串的变体。目前只有两个,Android和iOS。这里不禁有人要问,为什么没有全平台?嗯,信鸽就是没有全平台!!!
Audience //推送目标,或者说推送受众。这里有All,tag,token,token_list等等,详细看官方文档。
PushMsgOption //函数模型,用于快速扩展和修改PushMsg的某个属性
//大概介绍这么多

用法解析:

XgClient相关

XgClient作为推送操作的执行者,内部封装了信鸽推送的相关方法和逻辑。

创建XgClient

func NewXingeClient(androidAuth IAuth, iOSAuth IAuth, client *http.Client) XgClient
func NewXingeClient3() XgClient
func NewXingeClientent2(appId string, secretKey string, platform Platform) XgClient

XgClient的用法:

//这个方法用来解析返回的http.Response为通用的返回数据,不多解释
func (xg XgClient) MarshalResp(resp *http.Response) CommonRsp
//推送一条消息
// 这里需要注意的是,这个操作会比对msg中的Platform属性和XgClient自身是否持有相应平台的IAuth
// 如果XgClient没有相应的IAuth,则会抛出异常
func (xg XgClient) Push(msg IPushMsg) CommonRsp
//使用指定的IAuth信息来推送一条消息
func (xg XgClient) PushWithAuthorization(msg IPushMsg, auth IAuth) CommonRsp

//设置Android平台的IAuth信息
func (xg *XgClient) SetAndroidAuth(auth IAuth)
//设置iOS平台的IAuth信息
func (xg *XgClient) SetIOSAuth(auth IAuth)
//直接通过appId,secretKey,以及平台信息来设置某个平台(Android或者iOS)的IAuth
func (xg *XgClient) SetAuth(appId string, secretKey string, platform Platform)

如何创建推送消息?

这里提供了以下一些方法来帮助用户快速创建推送消息:

func DefaultPushMsg(platform Platform, msgType MessageType, title string, content string) IPushMsg
func NewAccountNotifyPushMsg(platform Platform, title string, content string, accounts ...string) IPushMsg
func NewAccountPushMsg(platform Platform, msgType MessageType, title string, content string, accounts ...string) IPushMsg
func NewPushAllNotifyPushMsg(platform Platform, title string, content string) IPushMsg
func NewPushAllPushMsg(platform Platform, msgType MessageType, title string, content string) IPushMsg
func NewTagNotifyPushMsg(platform Platform, title string, content string, tagOpt TagOperation, tags ...string) IPushMsg
func NewTagPushMsg(platform Platform, msgType MessageType, title string, content string, tagOpt TagOperation, tags ...string) IPushMsg
func NewTokenNotifyPushMsg(platform Platform, title string, content string, tokens ...string) IPushMsg
func NewTokenPushMsg(platform Platform, msgType MessageType, title string, content string, tokens ...string) IPushMsg

包括了所有推送类型的推送方法,需要重点说明的是,token和tokenlist,account和account_list的类型区分,都是依靠末尾的不定长度的参数个数决定的!

例如:只传递一个token进入NewTokenPushMsg函数,则类型为token,如果为多个,则为token_list。account同理!

重点说明:对于token_list和account_list类型的推送,如果推送数量超过1000,理论上会自动轮询推送,不需要再手动处理!请知悉!

如何修改推送的消息?

两种方法:

1.手动修改;
2.借助PushMsgOption进行修改;

第一种略过,我们说第二种,PushMsg具有一个非常好用的方法:

func (rst *PushMsg) RenderOptions(opts ...PushMsgOption) error

这个方法能使用PushMsgOption对象来修改自身的属性。详细请看源码实现!!!

如何创建PushMsgOption?

结合FrontMage大神写一些东西,做了修改和补充后,具有以下规模:

func OptionAccountList(al ...string) PushMsgOption
func OptionAccountListAdd(a string) PushMsgOption
func OptionAccountType(at int) PushMsgOption
func OptionAddAction(k string, v interface{}) PushMsgOption
func OptionAndroidParams(params *AndroidParams) PushMsgOption
func OptionAps(aps *Aps) PushMsgOption
func OptionApsAlert(alert Alert) PushMsgOption
func OptionApsBadage(badge int) PushMsgOption
func OptionApsCategory(category string) PushMsgOption
func OptionApsContentAvailable(contentAvailable int) PushMsgOption
func OptionApsSound(sound string) PushMsgOption
func OptionApsThreadId(threadId string) PushMsgOption
func OptionBuilderID(id int) PushMsgOption
func OptionCleanable(c int) PushMsgOption
func OptionContent(c string) PushMsgOption
func OptionCustomContent(ct map[string]string) PushMsgOption
func OptionCustomContentSet(k, v string) PushMsgOption
func OptionEnvDevelop() PushMsgOption
func OptionEnvProduction() PushMsgOption
func OptionExpireTime(et time.Time) PushMsgOption
func OptionIOSParams(params *IOSParams) PushMsgOption
func OptionIconRes(ir string) PushMsgOption
func OptionIconType(it int) PushMsgOption
func OptionLights(l int) PushMsgOption
func OptionLoopTimes(lt int) PushMsgOption
func OptionMessage(m Message) PushMsgOption
func OptionMessageType(t MessageType) PushMsgOption
func OptionMultiPkg(mp bool) PushMsgOption
func OptionNID(id int) PushMsgOption
func OptionPlatAndroid() PushMsgOption
func OptionPlatIos() PushMsgOption
func OptionPlatform(p Platform) PushMsgOption
func OptionPushID(pid string) PushMsgOption
func OptionRing(ring int) PushMsgOption
func OptionRingRaw(rr string) PushMsgOption
func OptionSendTime(st time.Time) PushMsgOption
func OptionSeq(s int64) PushMsgOption
func OptionSmallIcon(si int) PushMsgOption
func OptionStatTag(st string) PushMsgOption
func OptionStyleID(s int) PushMsgOption
func OptionTagList(op TagOperation, tags ...string) PushMsgOption
func OptionTagListOpt2(tl TagList) PushMsgOption
func OptionTitle(t string) PushMsgOption
func OptionTokenList(tl ...string) PushMsgOption
func OptionTokenListAdd(t string) PushMsgOption
func OptionVibrate(v int) PushMsgOption

基本上已经包含了所有可以修改的部分,具体每一个函数用来修改那一块,可以看源码,源码中都有注释!!!

特别说明:

修改和扩充了Alert对象,给Alert增加了一些操作方法。详细的请涉及到苹果推送的同学自己尝试(没有苹果开发者证书,这块没法做测试,sorry)

type Alert map[string]interface{}
func (alert Alert) Set(key string, value interface{})
func (alert Alert) SetActionLocKey(data string)
func (alert Alert) SetBody(content string)
func (alert Alert) SetLaunchImage(data string)
func (alert Alert) SetLocArgs(data []string)
func (alert Alert) SetLocKey(data string)
func (alert Alert) SetTitle(title string)
func (alert Alert) SetTitleLocArgs(data []string)
func (alert Alert) SetTitleLocKey(data string)

几个或许有用处的方法:

func DefaultApsAlert(title string, body string) Alert

func DefaultIOSParams(title string, content string) *IOSParams

func DefaultAndroidParams() *AndroidParams

func DefaultPushMsg(platform Platform, msgType MessageType, title string, content string) IPushMsg

相应的,如果想看特别详细的代码文档,点击文档顶部的godoc图标,或者是下面的图标

Go Report Card GoDoc

捐助作者

   
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

腾讯信鸽Golang SDK(非官方版本) 展开 收起
Go
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/kmlixh/xinge.git
git@gitee.com:kmlixh/xinge.git
kmlixh
xinge
xinge
master

搜索帮助