LoginSignup
8
7

More than 5 years have passed since last update.

Goでお手軽PUSH通知テスト

Posted at

GoでiOSにPUSH通知を簡単に送りたい人へ

ライブラリ

https://github.com/anachronistic/apns
https://github.com/timehop/apns
https://github.com/joekarl/go-libapns

(この他にも、まだまだあります...!

結論

timehopさんを使用しました。

最もスターが多いanachronisticさんのライブラリを最初は使っていたのですが、動作が遅かったので、issueを読んでいたら、どうも処理に間違いがあるようです。
https://github.com/anachronistic/apns/pull/14
こちらのPRがマージされれば治る気配があるようですが、、まだマージされていないようです。

joekarlさんのライブラリはまだ詳しく見れていないのですが、実装方法が面白そうなので載せておきました。

サンプルコード

push.go
package main

import (
    "log"

    "github.com/timehop/apns"
)

func main() {
    c, err := apns.NewClientWithFiles(apns.SandboxGateway, "apns-dev-cert.pem", "apns-dev-key-noenc.pem")
    if err != nil {
        log.Fatal("Could not create client: ", err.Error())
    }
    p := apns.NewPayload()
    p.APS.Alert.Body = "YO"

    m := apns.NewNotification()
    m.Payload = p
    m.DeviceToken = "x-x-x-x"
    m.Priority = apns.PriorityImmediate

    sendError := c.Send(m)

    if sendError == nil {
        log.Print("Send Success")
    } else {
        log.Fatal("Send Fail: ", sendError.Error())
    }
}

PEMファイルの作成については今回は割愛させていただきます。
Badgeなども付与できます。詳細はtimehopさんのgithubにあるサンプルを見てみてください。

実行

go get github.com/timehop/apns
go run push.go
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7