Merge pull request #40 from appleboy/command

fix #39 support command line to send notification.
This commit is contained in:
Bo-Yi Wu 2016-04-10 03:00:53 -05:00
commit 8ca03d74da
3 changed files with 86 additions and 5 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 [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 command line to send single Androdi or iOS notification.
* Support Web API to send push notification.
See the [YAML config eample](config/config.yaml):
@ -43,6 +45,34 @@ log:
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
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")
apiKey := flag.String("k", "", "Android api key configuration 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()
@ -55,13 +60,59 @@ func main() {
return
}
if err = gopush.CheckPushConf(); err != nil {
log.Println(err)
gopush.LogError.Fatal(err)
// send android notification
if *android {
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
}
// 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.RunHTTPServer()
}

View File

@ -1,10 +1,10 @@
package gopush
import (
"fmt"
api "github.com/appleboy/gin-status-api"
"github.com/gin-gonic/gin"
"net/http"
"fmt"
)
func AbortWithError(c *gin.Context, code int, message string) {
@ -26,7 +26,7 @@ func pushHandler(c *gin.Context) {
var msg string
if err := c.BindJSON(&form); err != nil {
msg = "Missing nitifications field."
msg = "Missing notifications field."
LogAccess.Debug(msg)
AbortWithError(c, http.StatusBadRequest, msg)
return