Skip to content

Commit 27783a1

Browse files
byungwoo-Namgoogle-oss-botlahirumaramba
authored
feat(fcm): Support Proxy field in FCM AndroidNotification (#676)
* [chore] Release 4.12.0 (#561) - Release 4.12.0 * Revert "[chore] Release 4.12.0 (#561)" (#565) This reverts commit 32af2b8. * Modifying the messaging package - Adding Proxy Fields to AndroidNotification Struct * fix the lint errors * add tests to verify * fix AndroidNotificationProxy unmarshalling problem --------- Co-authored-by: Google Open Source Bot <[email protected]> Co-authored-by: Lahiru Maramba <[email protected]> Co-authored-by: Google Open Source Bot <[email protected]>
1 parent 0a3a5b5 commit 27783a1

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

messaging/messaging.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ type AndroidNotification struct {
191191
DefaultLightSettings bool `json:"default_light_settings,omitempty"`
192192
Visibility AndroidNotificationVisibility `json:"-"`
193193
NotificationCount *int `json:"notification_count,omitempty"`
194+
Proxy AndroidNotificationProxy `json:"-"`
194195
}
195196

196197
// MarshalJSON marshals an AndroidNotification into JSON (for internal use only).
@@ -217,6 +218,16 @@ func (a *AndroidNotification) MarshalJSON() ([]byte, error) {
217218
visibility, _ = visibilities[a.Visibility]
218219
}
219220

221+
var proxy string
222+
if a.Proxy != proxyUnspecified {
223+
proxies := map[AndroidNotificationProxy]string{
224+
ProxyAllow: "ALLOW",
225+
ProxyDeny: "DENY",
226+
ProxyIfPriorityLowered: "IF_PRIORITY_LOWERED",
227+
}
228+
proxy, _ = proxies[a.Proxy]
229+
}
230+
220231
var timestamp string
221232
if a.EventTimestamp != nil {
222233
timestamp = a.EventTimestamp.UTC().Format(rfc3339Zulu)
@@ -232,12 +243,14 @@ func (a *AndroidNotification) MarshalJSON() ([]byte, error) {
232243
EventTimestamp string `json:"event_time,omitempty"`
233244
Priority string `json:"notification_priority,omitempty"`
234245
Visibility string `json:"visibility,omitempty"`
246+
Proxy string `json:"proxy,omitempty"`
235247
VibrateTimings []string `json:"vibrate_timings,omitempty"`
236248
*androidInternal
237249
}{
238250
EventTimestamp: timestamp,
239251
Priority: priority,
240252
Visibility: visibility,
253+
Proxy: proxy,
241254
VibrateTimings: vibTimings,
242255
androidInternal: (*androidInternal)(a),
243256
}
@@ -251,6 +264,7 @@ func (a *AndroidNotification) UnmarshalJSON(b []byte) error {
251264
EventTimestamp string `json:"event_time,omitempty"`
252265
Priority string `json:"notification_priority,omitempty"`
253266
Visibility string `json:"visibility,omitempty"`
267+
Proxy string `json:"proxy,omitempty"`
254268
VibrateTimings []string `json:"vibrate_timings,omitempty"`
255269
*androidInternal
256270
}{
@@ -288,6 +302,19 @@ func (a *AndroidNotification) UnmarshalJSON(b []byte) error {
288302
}
289303
}
290304

305+
if temp.Proxy != "" {
306+
proxies := map[string]AndroidNotificationProxy{
307+
"ALLOW": ProxyAllow,
308+
"DENY": ProxyDeny,
309+
"IF_PRIORITY_LOWERED": ProxyIfPriorityLowered,
310+
}
311+
if prox, ok := proxies[temp.Proxy]; ok {
312+
a.Proxy = prox
313+
} else {
314+
return fmt.Errorf("unknown proxy value: %q", temp.Proxy)
315+
}
316+
}
317+
291318
if temp.EventTimestamp != "" {
292319
ts, err := time.Parse(rfc3339Zulu, temp.EventTimestamp)
293320
if err != nil {
@@ -356,6 +383,23 @@ const (
356383
VisibilitySecret
357384
)
358385

386+
// AndroidNotificationProxy to control when a notification may be proxied.
387+
type AndroidNotificationProxy int
388+
389+
const (
390+
proxyUnspecified AndroidNotificationProxy = iota
391+
392+
// ProxyAllow tries to proxy this notification.
393+
ProxyAllow
394+
395+
// ProxyDeny does not proxy this notification.
396+
ProxyDeny
397+
398+
// ProxyIfPriorityLowered only tries to proxy this notification if its AndroidConfig's Priority was
399+
// lowered from high to normal on the device.
400+
ProxyIfPriorityLowered
401+
)
402+
359403
// LightSettings to control notification LED.
360404
type LightSettings struct {
361405
Color string

messaging/messaging_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,63 @@ var validMessages = []struct {
683683
"topic": "test-topic",
684684
},
685685
},
686+
{
687+
name: "AndroidNotificationProxyAllow",
688+
req: &Message{
689+
Android: &AndroidConfig{
690+
Notification: &AndroidNotification{
691+
Proxy: ProxyAllow,
692+
},
693+
},
694+
Topic: "test-topic",
695+
},
696+
want: map[string]interface{}{
697+
"android": map[string]interface{}{
698+
"notification": map[string]interface{}{
699+
"proxy": "ALLOW",
700+
},
701+
},
702+
"topic": "test-topic",
703+
},
704+
},
705+
{
706+
name: "AndroidNotificationProxyDeny",
707+
req: &Message{
708+
Android: &AndroidConfig{
709+
Notification: &AndroidNotification{
710+
Proxy: ProxyDeny,
711+
},
712+
},
713+
Topic: "test-topic",
714+
},
715+
want: map[string]interface{}{
716+
"android": map[string]interface{}{
717+
"notification": map[string]interface{}{
718+
"proxy": "DENY",
719+
},
720+
},
721+
"topic": "test-topic",
722+
},
723+
},
724+
{
725+
name: "AndroidNotificationProxyIfPriorityLowered",
726+
req: &Message{
727+
Android: &AndroidConfig{
728+
Notification: &AndroidNotification{
729+
Proxy: ProxyIfPriorityLowered,
730+
},
731+
},
732+
Topic: "test-topic",
733+
},
734+
want: map[string]interface{}{
735+
"android": map[string]interface{}{
736+
"notification": map[string]interface{}{
737+
"proxy": "IF_PRIORITY_LOWERED",
738+
},
739+
},
740+
"topic": "test-topic",
741+
},
742+
},
686743
}
687744

688745
var invalidMessages = []struct {

0 commit comments

Comments
 (0)