资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Go语言

gorush消息推送使用

Go语言 更新时间: 发布时间: 计算机考试归档 最新发布

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: %tn", r.Success)
	log.Printf("Count: %dn", 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

转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/991474.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【gorush消息推送使用】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2