fix #39 support command line to send notification.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-10 16:00:17 +08:00
parent f438873e51
commit d4df55dfd6
3 changed files with 85 additions and 4 deletions

View File

@ -9,6 +9,8 @@ A push notification server using [Gin](https://github.com/gin-gonic/gin) framewo
* Support [Google Cloud Message](https://developers.google.com/cloud-messaging/) using [go-gcm](https://github.com/google/go-gcm) library for Android. * Support [Google Cloud Message](https://developers.google.com/cloud-messaging/) using [go-gcm](https://github.com/google/go-gcm) library for Android.
* Support [HTTP/2](https://http2.github.io/) Apple Push Notification Service using [apns2](https://github.com/sideshow/apns2) library. * Support [HTTP/2](https://http2.github.io/) Apple Push Notification Service using [apns2](https://github.com/sideshow/apns2) library.
* Support [YAML](https://github.com/go-yaml/yaml) configuration. * Support [YAML](https://github.com/go-yaml/yaml) configuration.
* Support command line to send single Androdi or iOS notification.
* Support Web API to send push notification.
See the [YAML config eample](config/config.yaml): See the [YAML config eample](config/config.yaml):
@ -43,6 +45,34 @@ log:
error_level: "error" error_level: "error"
``` ```
## Basic Usage
How to send push notification with `gopush` command line? (Android or iOS)
### Android
Send single notification with the following command.
```bash
$ ./gopush -android -m="your message" -k="API Key" -t="Device token"
```
* `-m`: Notification message.
* `-k`: [Google cloud message](https://developers.google.com/cloud-messaging/) api key
* `-t`: Device token.
### iOS
Send single notification with the following command.
```bash
$ ./gopush -ios -m="your message" -i="API Key" -t="Device token"
```
* `-m`: Notification message.
* `-i`: Apple Push Notification Certificate path (`pem` file).
* `-t`: Device token.
## License ## License
Copyright 2016 Bo-Yi Wu [@appleboy](https://twitter.com/appleboy). Copyright 2016 Bo-Yi Wu [@appleboy](https://twitter.com/appleboy).

View File

@ -12,6 +12,11 @@ func main() {
certificateKeyPath := flag.String("i", "", "iOS certificate key file path for gopush") certificateKeyPath := flag.String("i", "", "iOS certificate key file path for gopush")
apiKey := flag.String("k", "", "Android api key configuration for gopush") apiKey := flag.String("k", "", "Android api key configuration for gopush")
port := flag.String("p", "", "port number for gopush") port := flag.String("p", "", "port number for gopush")
token := flag.String("t", "", "token string")
message := flag.String("m", "", "notification message")
android := flag.Bool("android", false, "send android notification")
ios := flag.Bool("ios", false, "send ios notification")
production := flag.Bool("production", false, "production mode in iOS")
flag.Parse() flag.Parse()
@ -55,13 +60,59 @@ func main() {
return return
} }
if err = gopush.CheckPushConf(); err != nil { // send android notification
log.Println(err) if *android {
gopush.LogError.Fatal(err) if len(*token) == 0 {
gopush.LogError.Fatal("Missing token flag (-t)")
}
if len(*message) == 0 {
gopush.LogError.Fatal("Missing message flag (-m)")
}
gopush.PushConf.Android.Enabled = true
req := gopush.PushNotification{
Tokens: []string{*token},
Platform: gopush.PlatFormAndroid,
Message: *message,
}
gopush.PushToAndroid(req)
return return
} }
// send android notification
if *ios {
if len(*token) == 0 {
gopush.LogError.Fatal("Missing token flag (-t)")
}
if len(*message) == 0 {
gopush.LogError.Fatal("Missing message flag (-m)")
}
if *production {
gopush.PushConf.Ios.Production = true
}
gopush.PushConf.Ios.Enabled = true
req := gopush.PushNotification{
Tokens: []string{*token},
Platform: gopush.PlatFormIos,
Message: *message,
}
gopush.InitAPNSClient()
gopush.PushToIOS(req)
return
}
if err = gopush.CheckPushConf(); err != nil {
gopush.LogError.Fatal(err)
}
gopush.InitAPNSClient() gopush.InitAPNSClient()
gopush.RunHTTPServer() gopush.RunHTTPServer()
} }

View File

@ -1,10 +1,10 @@
package gopush package gopush
import ( import (
"fmt"
api "github.com/appleboy/gin-status-api" api "github.com/appleboy/gin-status-api"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"fmt"
) )
func AbortWithError(c *gin.Context, code int, message string) { func AbortWithError(c *gin.Context, code int, message string) {