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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值