分享一篇go实现的IOS的站外推送 APNS

package main

import (
	"encoding/json"
	"fmt"
	"github.com/RobotsAndPencils/buford/certificate"
	"github.com/RobotsAndPencils/buford/payload"
	"github.com/RobotsAndPencils/buford/payload/badge"
	"github.com/RobotsAndPencils/buford/push"
	"os"
)

// set these variables appropriately
const (
	filename = ""
	password = ""
	//host = push.Development
	host        = push.Development
	deviceToken = ""
)

func main() {
	// load a certificate and use it to connect to the go get APN service:
	cert, err := certificate.Load(filename, password)
	exitOnError(err)

	topic := "32442424"
	if err == nil {
		fmt.Printf("err is nil ****\n")
		fmt.Printf("@@@@@@@@@@@@@@@@@@@????????????????????\n")
		topic = certificate.TopicFromCert(cert)
		fmt.Printf("topic: %s\n", topic)
	}

	client, err := push.NewClient(cert)
	exitOnError(err)

	service := push.NewService(client, host)

	// construct a payload to send to the device:
	p := payload.APS{
		Alert: payload.Alert{Body: "Hello HTTP/2"},
		Badge: badge.New(42),
	}
	b, err := json.Marshal(p)
	fmt.Println("json.Marshal")
	exitOnError(err)

	// push the notification:

	  //headers := Headers{
	  //ID:          "uuid",
	  //CollapseID:  "game1.score.identifier",
	  //Expiration:  time.Unix(12622780800, 0),
	  //LowPriority: true,
	  //Topic:       "bundle-id",
	  //}




	var headers push.Headers
	fmt.Printf("topic: %s\n", topic)
	if topic != "" {
		headers = push.Headers{
			Topic: topic,
		}
		fmt.Printf("the Topic is *** %s\n", headers.Topic)
	}

	id, err := service.Push(deviceToken, &headers, b)
	//id, err := service.Push(deviceToken, nil, b)
	fmt.Println("service.Push")
	exitOnError(err)

	fmt.Println("apns-id:", id)
}

func exitOnError(err error) {
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

上面是代码,引用的是 https://github.com/bergusman/apns-go

这个库的方法。上面需要用p12的证书,token添加推送手机token。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值