Skip to content

Commit

Permalink
Merge pull request #1 from chrishaines/relevance-score-zero
Browse files Browse the repository at this point in the history
Allow relevance-score to be set to zero.
  • Loading branch information
neilmorton committed Sep 3, 2021
2 parents 2b6c257 + b7634f4 commit 5485b9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion payload/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type aps struct {
ContentAvailable int `json:"content-available,omitempty"`
InterruptionLevel string `json:"interruption-level,omitempty"`
MutableContent int `json:"mutable-content,omitempty"`
RelevanceScore float32 `json:"relevance-score,omitempty"`
RelevanceScore interface{} `json:"relevance-score,omitempty"`
Sound interface{} `json:"sound,omitempty"`
ThreadID string `json:"thread-id,omitempty"`
URLArgs []string `json:"url-args,omitempty"`
Expand Down Expand Up @@ -378,6 +378,17 @@ func (p *Payload) RelevanceScore(b float32) *Payload {
return p
}

// Unsets the relevance score
// that the system uses to sort the notifications from your app.
// The highest score gets featured in the notification summary.
// See https://developer.apple.com/documentation/usernotifications/unnotificationcontent/3821031-relevancescore.
//
// {"aps":{"relevance-score":0.1}}
func (p *Payload) UnsetRelevanceScore() *Payload {
p.aps().RelevanceScore = nil
return p
}

// MarshalJSON returns the JSON encoded version of the Payload
func (p *Payload) MarshalJSON() ([]byte, error) {
return json.Marshal(p.content)
Expand Down
12 changes: 12 additions & 0 deletions payload/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,15 @@ func TestRelevanceScore(t *testing.T) {
b, _ := json.Marshal(payload)
assert.Equal(t, `{"aps":{"relevance-score":0.1}}`, string(b))
}

func TestRelevanceScoreZero(t *testing.T) {
payload := NewPayload().RelevanceScore(0)
b, _ := json.Marshal(payload)
assert.Equal(t, `{"aps":{"relevance-score":0}}`, string(b))
}

func TestUnsetRelevanceScore(t *testing.T) {
payload := NewPayload().RelevanceScore(0.1).UnsetRelevanceScore()
b, _ := json.Marshal(payload)
assert.Equal(t, `{"aps":{}}`, string(b))
}

0 comments on commit 5485b9d

Please sign in to comment.