golang实现Gaurun推送服务的客户端插件gaurun-client的使用

gaurun-client

Gaurun 客户端插件,使用 Go 语言实现。

关于

这是一个用 Go 编写的 Gaurun 客户端库。

安装

$ go get -u github.com/osamingo/gaurun-client

使用示例

下面是一个完整的示例代码,展示如何使用 gaurun-client 发送推送通知:

package main

import (
	"log"

	"github.com/osamingo/gaurun-client"
)

func main() {
	// 创建 Gaurun 客户端实例
	client := gaurun.NewClient("http://your-gaurun-server:port")

	// 构建 iOS 推送通知
	iosNotification := &gaurun.Request{
		Notifications: []gaurun.Notification{
			{
				// 必填字段
				Token:   "ios_device_token",
				Message: "Hello iOS!",
				
				// 可选字段
				Title:    "Notification Title",
				Sound:    "default",
				Badge:    1,
				Category: "ACTION_CATEGORY",
				
				// 自定义字段
				Extend: map[string]interface{}{
					"key1": "value1",
					"key2": 123,
				},
			},
		},
	}

	// 发送 iOS 推送
	iosResp, err := client.Push(iosNotification)
	if err != nil {
		log.Fatalf("Failed to send iOS push: %v", err)
	}
	log.Printf("iOS push response: %+v", iosResp)

	// 构建 Android 推送通知
	androidNotification := &gaurun.Request{
		Notifications: []gaurun.Notification{
			{
				// 必填字段
				Token:   "android_device_token",
				Message: "Hello Android!",
				
				// 可选字段
				Title: "Notification Title",
				
				// 自定义字段
				Extend: map[string]interface{}{
					"key1": "value1",
					"key2": 123,
				},
			},
		},
	}

	// 发送 Android 推送
	androidResp, err := client.Push(androidNotification)
	if err != nil {
		log.Fatalf("Failed to send Android push: %v", err)
	}
	log.Printf("Android push response: %+v", androidResp)
}

代码说明

  1. 首先创建 Gaurun 客户端实例,需要传入 Gaurun 服务器的地址
  2. 构建通知请求时,需要设置:
    • Token: 设备令牌
    • Message: 推送消息内容
    • 其他可选字段如标题、声音、角标等
  3. 可以添加自定义字段到 Extend map 中
  4. 调用 Push() 方法发送通知

错误处理

Push() 方法可能返回以下错误:

  • 网络连接问题
  • 服务器返回非200状态码
  • 响应解析错误

许可证

本项目使用 MIT 许可证。


更多关于golang实现Gaurun推送服务的客户端插件gaurun-client的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现Gaurun推送服务的客户端插件gaurun-client的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Gaurun客户端插件使用指南

Gaurun是一个用Go编写的轻量级推送通知服务器,支持APNs(iOS)和FCM(Android)。下面我将介绍如何使用Gaurun的客户端插件gaurun-client来发送推送通知。

安装gaurun-client

首先需要安装gaurun-client包:

go get github.com/mercari/gaurun/gaurun-client

基本使用示例

package main

import (
	"log"
	
	"github.com/mercari/gaurun/gaurun-client"
)

func main() {
	// 创建客户端实例
	client := gaurun.NewClient("http://your-gaurun-server:1056")

	// 创建iOS推送请求
	iosReq := &gaurun.RequestGaurunNotification{
		Platform: gaurun.PlatformIos,
		Token:    "ios_device_token",
		Message:  "Hello iOS!",
		Badge:    1,
		Sound:    "default",
	}

	// 发送iOS推送
	resp, err := client.Push(iosReq)
	if err != nil {
		log.Fatalf("Failed to send iOS push: %v", err)
	}
	log.Printf("iOS push response: %+v", resp)

	// 创建Android推送请求
	androidReq := &gaurun.RequestGaurunNotification{
		Platform: gaurun.PlatformAndroid,
		Token:    "android_device_token",
		Message:  "Hello Android!",
	}

	// 发送Android推送
	resp, err = client.Push(androidReq)
	if err != nil {
		log.Fatalf("Failed to send Android push: %v", err)
	}
	log.Printf("Android push response: %+v", resp)
}

高级功能

批量推送

func sendBatchPush(client *gaurun.Client) {
	// 创建批量请求
	batchReq := &gaurun.RequestGaurunNotificationBatch{
		Notifications: []gaurun.RequestGaurunNotification{
			{
				Platform: gaurun.PlatformIos,
				Token:   "ios_token_1",
				Message: "Batch message 1",
			},
			{
				Platform: gaurun.PlatformAndroid,
				Token:   "android_token_1",
				Message: "Batch message 2",
			},
		},
	}

	// 发送批量推送
	resp, err := client.PushBatch(batchReq)
	if err != nil {
		log.Fatalf("Failed to send batch push: %v", err)
	}
	log.Printf("Batch push response: %+v", resp)
}

自定义选项

func sendCustomPush(client *gaurun.Client) {
	req := &gaurun.RequestGaurunNotification{
		Platform: gaurun.PlatformIos,
		Token:    "ios_device_token",
		Message:  "Custom push",
		
		// iOS特有选项
		Badge:           5,
		Sound:           "custom_sound.aiff",
		ContentAvailable: true,
		Expiry:          3600, // 1小时后过期
		
		// Android特有选项
		CollapseKey:    "update",
		DelayWhileIdle: true,
		TimeToLive:     3600,
		
		// 通用选项
		Title:          "Important",
		Subtitle:       "Update available",
		Category:       "NEWS",
		MutableContent: true,
	}

	resp, err := client.Push(req)
	if err != nil {
		log.Fatalf("Failed to send custom push: %v", err)
	}
	log.Printf("Custom push response: %+v", resp)
}

错误处理

func handleErrors(client *gaurun.Client) {
	req := &gaurun.RequestGaurunNotification{
		Platform: 3, // 无效平台
		Token:    "invalid_token",
		Message:  "This will fail",
	}

	resp, err := client.Push(req)
	if err != nil {
		// 检查是否是Gaurun返回的错误
		if gaurunErr, ok := err.(*gaurun.ErrorResponse); ok {
			log.Printf("Gaurun error: Code=%d, Message=%s", gaurunErr.Code, gaurunErr.Message)
		} else {
			log.Printf("Network/other error: %v", err)
		}
		return
	}
	log.Printf("Response: %+v", resp)
}

最佳实践

  1. 复用客户端:创建一次客户端并复用,而不是每次推送都新建
  2. 异步发送:对于大量推送,使用goroutine异步发送
  3. 错误重试:实现简单的重试逻辑处理临时性错误
  4. 性能监控:记录推送耗时和成功率
func asyncPush(client *gaurun.Client, notifications []gaurun.RequestGaurunNotification) {
	for _, notif := range notifications {
		go func(n gaurun.RequestGaurunNotification) {
			// 简单重试逻辑
			var lastErr error
			for i := 0; i < 3; i++ {
				_, err := client.Push(&n)
				if err == nil {
					return
				}
				lastErr = err
				time.Sleep(time.Second * time.Duration(i+1))
			}
			log.Printf("Failed to send push after retries: %v", lastErr)
		}(notif)
	}
}

通过以上示例,您可以快速集成Gaurun推送服务到您的Go应用中。根据实际需求调整参数和错误处理逻辑即可。

回到顶部