gorush消息推送使用

gorush使用

1.介绍

github:https://github.com/appleboy/gorush

Gorush 是一套使用 Gin framework 编写的消息推送服务器。支援Android的FCM及苹果的APNS,还有华为的HMS推送。

2.下载

Go语言使用

go get -u -v github.com/appleboy/gorush

linux

wget https://github.com/appleboy/gorush/releases/download/v1.13.0/gorush-v1.13.0-linux-amd64 -O gorush

OS X

wget https://github.com/appleboy/gorush/releases/download/v1.13.0/gorush-v1.13.0-darwin-amd64 -O gorush

Windows

wget https://github.com/appleboy/gorush/releases/download/v1.13.0/gorush-v1.13.0-windows-amd64.exe -O gorush.exe

macOS, use Homebrew.

brew install --HEAD https://github.com/appleboy/gorush/raw/master/HomebrewFormula/gorush.rb

3.获取从GitHub

Gorush使用内置在Go 1.11中的Go模块进行构建。最简单的方法是在GOPATH之外的一个目录下克隆Gorush,如下例所示:

mkdir $HOME/src
cd $HOME/src
git clone https://github.com/appleboy/gorush.git
cd gorush
go install

4.gorush使用

(1)linux 启动gorush进程服务,提供对外grpc接口调用

启动(支持ios apns)

GORUSH_GRPC_ENABLED=true GORUSH_GRPC_PORT=3000  GORUSH_IOS_PRODUCTION=true GORUSH_IOS_ENABLED=true GORUSH_IOS_KEY_PATH=../config/cert.pem   gorush  
  • GORUSH_GRPC_ENABLED=true:启动grpc服务模式,

  • GORUSH_GRPC_PORT=3000 :grpc服务监听3000端口

  • GORUSH_IOS_PRODUCTION=true :使用生成模式

  • GORUSH_IOS_ENABLED=true :开启ios接受

  • GORUSH_IOS_KEY_PATH=…/config/cert.pem :anps证书目录

grpc服务pb协议为:https://github.com/appleboy/gorush/blob/master/rpc/proto/gorush.proto

(2)Client调用示例:
package main

import (
	"context"
	"log"

	"github.com/appleboy/gorush/rpc/proto"

	structpb "github.com/golang/protobuf/ptypes/struct"
	"google.golang.org/grpc"
)

const (
	address = "localhost:9000"
)

func main() {
     
	// Set up a connection to the server.
	conn, err := grpc.Dial(address, grpc.WithInsecure())
	if err != nil {
     
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := proto.NewGorushClient(conn)

	r, err := c.Send(context.Background(), &proto.NotificationRequest{
     
		Platform: 2,
		Tokens:   []string{
     "1234567890"},
		Message:  "test message",
		Badge:    1,
		Category: "test",
		Sound:    "test",
		Priority: proto.Priority_High,
		Alert: &proto.Alert{
     
			Title:    "Test Title",
			Body:     "Test Alert Body",
			Subtitle: "Test Alert Sub Title",
			LocKey:   "Test loc key",
			LocArgs:  []string{
     "test", "test"},
		},
		Data: &structpb.Struct{
     
			Fields: map[string]*structpb.Value{
     
				"key1": {
     
					Kind: &structpb.Value_StringValue{
     StringValue: "welcome"},
				},
				"key2": {
     
					Kind: &structpb.Value_NumberValue{
     NumberValue: 2},
				},
			},
		},
	})
	if err != nil {
     
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Success: %t\n", r.Success)
	log.Printf("Count: %d\n", r.Counts)
}

(3)linux 命令直接调用推送接口

命令启动

ios设备:

gorush -ios -m "test message hi" -i \
"../config/cert.pem"  \
 -t "abcdefg1234567fxxxxxxxxxxxxxxxxxxxxxxxx" \
 --topic "com.xxx.xxx.ios" -production

安卓设备:

gorush -android -m "test android push message hi" \
-t "xxxxxxxxxxxxxxxxxxxxxxx" 
-k "xxxxxxxxxxxxxxxxxx"
  • -i : 调用的证书路径

  • -t : 手机设备device token

  • –topoc:app对应的主题

  • -k:安卓api key

你可能感兴趣的:(go,go)