Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The JSON below is the request-body example.
"platform" : 2,
"message" : "Hello, Android!",
"collapse_key" : "update",
"priority" : "normal",
"delay_while_idle" : true,
"time_to_live" : 10
}
Expand All @@ -50,23 +51,24 @@ The JSON below is the request-body example.

The request-body must has the `notifications` array. There is the parameter table for each notification below.

|name |type |description |required|default|note |
|-----------------|------------|-----------------------------------------|--------|-------|----------------|
|token |string array|device tokens |o | | |
|platform |int |platform(iOS,Android) |o | |1=iOS, 2=Android|
|message |string |message for notification |o | | |
|title |string |title for notification |- | |only iOS |
|subtitle |string |subtitle for notification |- | |only iOS |
|badge |int |badge count |- |0 |only iOS |
|category |string |unnotification category |- | |only iOS |
|sound |string |sound type |- | |only iOS |
|expiry |int |expiration for notification |- |0 |only iOS. |
|content_available|bool |indicate that new content is available |- |false |only iOS. |
|mutable_content |bool |enable Notification Service app extension|- |false |only iOS(10.0+).|
|collapse_key |string |the key for collapsing notifications |- | |only Android |
|delay_while_idle |bool |the flag for device idling |- |false |only Android |
|time_to_live |int |expiration of message kept on GCM storage|- |0 |only Android |
|extend |string array|extensible partition |- | | |
|name |type |description |required|default|note |
|-----------------|------------|----------------------------------------------------|--------|-------|----------------|
|token |string array|device tokens |o | | |
|platform |int |platform(iOS,Android) |o | |1=iOS, 2=Android|
|message |string |message for notification |o | | |
|title |string |title for notification |- | |only iOS |
|subtitle |string |subtitle for notification |- | |only iOS |
|badge |int |badge count |- |0 |only iOS |
|category |string |unnotification category |- | |only iOS |
|sound |string |sound type |- | |only iOS |
|expiry |int |expiration for notification |- |0 |only iOS. |
|content_available|bool |indicate that new content is available |- |false |only iOS. |
|mutable_content |bool |enable Notification Service app extension |- |false |only iOS(10.0+).|
|collapse_key |string |the key for collapsing notifications |- | |only Android |
|delay_while_idle |bool |the flag for device idling |- |false |only Android |
|time_to_live |int |expiration of message kept on GCM storage |- |0 |only Android |
|priority |string |[high|normal] to deliver immediately or save battery|- |high |only Android |
|extend |string array|extensible partition |- | | |

The JSON below is the response-body example from Gaurun. In this case, the status is 200(OK).

Expand Down
1 change: 1 addition & 0 deletions cmd/gaurun_recover/gaurun_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func pushNotificationAndroid(req gaurun.RequestGaurunNotification) bool {
data := map[string]interface{}{"message": req.Message}
msg := gcm.NewMessage(data, req.Tokens...)
msg.CollapseKey = req.CollapseKey
msg.Priority = req.Priority
msg.DelayWhileIdle = req.DelayWhileIdle
msg.TimeToLive = req.TimeToLive

Expand Down
2 changes: 2 additions & 0 deletions gaurun/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type RequestGaurunNotification struct {
CollapseKey string `json:"collapse_key,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TimeToLive int `json:"time_to_live,omitempty"`
Priority string `json:"priority,omitempty"`
// iOS
Title string `json:"title,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Expand Down Expand Up @@ -132,6 +133,7 @@ func pushNotificationAndroid(req RequestGaurunNotification) error {
msg.CollapseKey = req.CollapseKey
msg.DelayWhileIdle = req.DelayWhileIdle
msg.TimeToLive = req.TimeToLive
msg.Priority = req.Priority

stime := time.Now()
resp, err := GCMClient.SendNoRetry(msg)
Expand Down
5 changes: 5 additions & 0 deletions gcm/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Message struct {
RegistrationIDs []string `json:"registration_ids"`
CollapseKey string `json:"collapse_key,omitempty"`
Priority string `json:"priority,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TimeToLive int `json:"time_to_live,omitempty"`
Expand All @@ -34,6 +35,10 @@ func (m *Message) validate() error {
return fmt.Errorf("the message's RegistrationIDs field must not be nil")
}

if string(m.Priority) != "" && m.Priority != "normal" && m.Priority != "high" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to cast? m.Priority is a string type. m.Priority is assigned "" as a zero value.

return fmt.Errorf("the message's Priority field is invalid")
}

if len(m.RegistrationIDs) == 0 {
return fmt.Errorf("the message must specify at least one registration ID")
}
Expand Down
36 changes: 36 additions & 0 deletions gcm/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ func TestValidateMessage(t *testing.T) {
},
false,
},

// test should pass when Priority is empty
{
&Message{
RegistrationIDs: []string{"1"},
Priority: "",
},
true,
},

// test should pass when Priority is high
{
&Message{
RegistrationIDs: []string{"1"},
Priority: "high",
},
true,
},

// test should pass when Priority is normal
{
&Message{
RegistrationIDs: []string{"1"},
Priority: "normal",
},
true,
},

// test should fail when message Priority is not high nor normal
{
&Message{
RegistrationIDs: []string{"1"},
Priority: "not_high_nor_normal",
},
false,
},
}

for i, tc := range cases {
Expand Down