push

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 2 Imported by: 0

README

[APP消息推送]

极光(JPush)

谷歌FCM(FireBase)

苹果APNs

信鸽(XinGe)

支持 FCM/HUAWEI/XIAOMI/MEIZU 厂商通道(需在信鸽开发者平台开启厂商通道并配置相关厂商的AppKey+AppSecret并且APP端需集成相关厂商的推送SDK)

友盟推送

支持 HUAWEI/XIAOMI/MEIZU/VIVO/OPPO(不支持Oppo v8.0以上系统) 厂商通道(服务端需在发送推送通知报文中指定mipush=true并在友盟开发者平台配置相关厂商的AppKey+AppSecret并且APP端需集成相关厂商的推送SDK)

代码示例


package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/push"
	_ "github.com/civet148/gotools/push/apns"
	_ "github.com/civet148/gotools/push/fcm"
	_ "github.com/civet148/gotools/push/jpush"
	_ "github.com/civet148/gotools/push/umeng"
	_ "github.com/civet148/gotools/push/xinge"
)

type PushExtra struct {
	Type   int32 `json:"type"`
	ChatID int32 `json:"chat_id"`
}

func main() {

	//JPushMessage()  //JPUSH(极光)
	//FcmMessage()    //FCM(Google FireBase)
	//UmengMessage()  //Umeng(友盟)
	XingeMessage()  //信鸽(tencent)
	//ApnsMessage()   //APNs(Apple)
}

func JPushMessage() {


	var strAppKey string = "2978da262aa372ed199901d2"
	var strSecret string = "1033636bfff8d246294fd1c8"
	var strToken string = "191e35f7e02aeaa0597" //极光token/registered_id 19字节
	var isProdEnv = false //sandbox environment is false, production is true for jpush

	jMsg := push.Message{
		AudienceType: push.AUDIENCE_TYPE_REGID_TOKEN,
		Audiences:    []string{strToken},
		Title:        "this is message title",
		Alert:        "you have a new message",
		Extra:        &PushExtra{Type: 1, ChatID: 10086},
	}

	JPUSH, err := push.New(push.AdapterType_JPush, strAppKey, strSecret, isProdEnv)
	if err != nil {
		log.Error("%v", err.Error())
		return
	}
	if JPUSH != nil {
		JPUSH.Debug(true)
		MsgID, err := JPUSH.Push(&jMsg)
		log.Debug("JPUSH msg id [%v] error [%v]", MsgID, err)
	}

}

func FcmMessage() {
	//fake api key, access 'console.firebase.google.com' to get your api key
	strApiKey := "AIzaSyBtMplqJkuTIDyIx_CM74MoPHbxHCBcY-o"
	strToken := "fpICefK-jfE:APA11bHjZTxe503tpFoFCmXXX9LAiMmg7OwgTPYmTb8Ox-yF88umTQnmTQUGbALplxqre7R6v3d0-vSK5MyT4jFtSqklbY1GIaM4d8uZ0wJlwWrRWdBDeOJ4rlpvamd3aGyBlHKAH18N"
	// test for FCM
	fcmMsg := push.Message{
		AudienceType: push.AUDIENCE_TYPE_REGID_TOKEN,
		Audiences:    []string{strToken},
		Title:        "this is message title",
		Alert:        "you have a new message",
		Extra:        &PushExtra{Type: 1, ChatID: 10086},
	}
	FCM, err := push.New(push.AdapterType_Fcm, strApiKey)
	if err != nil {
		log.Error("%v", err.Error())
		return
	}
	if FCM != nil {
		FCM.Debug(true)
		MsgID, err := FCM.Push(&fcmMsg)
		log.Debug("FCM msg id [%v] error [%v]", MsgID, err)
	}
}

func ApnsMessage() {
	var strAuthKeyFile = "AuthKey_U4Q9F3Y9WH.p8" //APNs JWT token auth key file (.p8)
	var strKeyID = "U4Q9F3Y9WH"                  //APNs key id
	var strTeamID = "2965AR985S"                 //APNs team id
	var strTopic = "com.chatol.thunderchat"      //bundle id of your app
	var strToken = "47b9ec1cb4ea5d9e6a62edb183e425a3f2b44715aa26d9848089b1d548e0a0b5" //iOS 64字节
	apnsMsg := push.Message{
		AudienceType: push.AUDIENCE_TYPE_REGID_TOKEN,
		Audiences:    []string{strToken},
		Title:        "this is message title",
		Alert:        "you have a new message",
		Extra:        &PushExtra{Type: 1, ChatID: 10086},
	}
	APNs, err := push.New(push.AdapterType_Apns, strAuthKeyFile, strKeyID, strTeamID, strTopic)
	if err != nil {
		log.Error("%v", err.Error())
		return
	}
	if APNs != nil {
		APNs.Debug(true)
		MsgID, err := APNs.Push(&apnsMsg)
		log.Debug("APNs msg id [%v] error [%v]", MsgID, err)
	}
}

func UmengMessage() {
	
	var strAppKey = "5e1qfcp30cxfb29u570k01f3"
	var strAppSecret = "jynhjnhhlcgt298ke9q6abpwz3n1j1pv"
	var strToken = "6a17coue30csp91mxy8zafb29f570001f3yaxhw913lx" //iOS 64字节 安卓 44字节
	Umeng, err := push.New(push.AdatperType_Umeng, strAppKey, strAppSecret)
	if err != nil {
		log.Error("%v", err.Error())
		return
	}
	if Umeng != nil {
		Umeng.Push(&push.Message{
			AudienceType: push.AUDIENCE_TYPE_REGID_TOKEN,
			Audiences:    []string{strToken},
			Platforms:    nil,
			Title:        "this is message title",
			Alert:        "you have a new message",
			SoundIOS:     "",
			SoundAndroid: 0,
			Extra:         &PushExtra{Type: 1, ChatID: 10086},
		})
	}
}

func XingeMessage() {


	var strAppKey string = "b1b86bd2a16b8"
	var strSecret string = "zbzzc05cfc8317ea224ze4b5c367z737"
	var strToken string = "6a17coue30csp91mxy8zafb29f570001f3yaxhwz" //iOS 64字节 安卓40字节
	var isProdEnv = false //sandbox environment is false, production is true for iOS

	msg := push.Message{
		AudienceType: push.AUDIENCE_TYPE_REGID_TOKEN,
		Audiences:    []string{strToken},
		Title:        "this is message title",
		Alert:        "you have a new message",
		Extra:        &PushExtra{Type: 1, ChatID: 10086},
	}

	XinGe, err := push.New(push.AdapterType_XinGe, strAppKey, strSecret, isProdEnv)
	if err != nil {
		log.Error("%v", err.Error())
		return
	}
	if XinGe != nil {
		XinGe.Debug(true)
		MsgID, err := XinGe.Push(&msg)
		log.Debug("XinGe msg id [%v] error [%v]", MsgID, err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HTTP_METHOD_GET    = "GET"
	HTTP_METHOD_POST   = "POST"
	HTTP_METHOD_DELETE = "DELETE"
	HTTP_METHOD_UPLOAD = "UPLOAD"
)
View Source
var (
	PLATFORM_ALL      = "all"
	PLATFORM_ANDROID  = "android"
	PLATFORM_IOS      = "ios"
	PLATFORM_WINPHONE = "winphone"
)
View Source
var AdapterMap = make(map[AdapterType]Instance)

Functions

func Register

func Register(adapter AdapterType, ins Instance) (err error)

register adapter instance

func StructToMap

func StructToMap(v interface{}) (m map[string]interface{})

Types

type AdapterType

type AdapterType int
var (
	AdapterType_JPush  AdapterType = 1 //极光推送
	AdapterType_Fcm    AdapterType = 2 //谷歌推送
	AdapterType_Apns   AdapterType = 3 //苹果推送
	AdatperType_Umeng  AdapterType = 4 //友盟推送
	AdapterType_XinGe  AdapterType = 5 //信鸽推送
	AdatperType_Huawei AdapterType = 6 //华为推送
)

func (AdapterType) GoString

func (t AdapterType) GoString() (name string)

func (AdapterType) String

func (t AdapterType) String() (name string)

type AudienceType

type AudienceType int
var (
	AUDIENCE_TYPE_REGID_TOKEN AudienceType = 1 //按设备注册ID/token推送(device register id or device token)
	AUDIENCE_TYPE_TAG         AudienceType = 2 //按标签推送(message group)
)

type IPush

type IPush interface {
	//push message to device
	PushNotification(msg *Notification) (MsgID string, err error)
	//enable or disable debug output
	Debug(enable bool)
}

func New added in v1.1.0

func New(adapter AdapterType, args ...interface{}) (IPush, error)

get adapter instance with args...

type Instance

type Instance func(args ...interface{}) IPush

type Notification

type Notification struct {
	AudienceType AudienceType //推送类型
	Audiences    []string     //设备注册ID或标签或别名
	Platforms    []string     //推送平台(可为空)
	Title        string       //标题
	Alert        string       //内容
	SoundIOS     string       //iOS声音设置 无 默认 自定义(空字符串、声音文件名、default)
	SoundAndroid int          //声音设置(0 默认 1 播放声音文件)
	Badge        int          //角标
	Extra        PushExtra    //扩展结构,用于点击推送消息自动跳转页面(选填)
}

type PushExtra

type PushExtra map[string]string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL