[Important] allow sending data ONLY notifications for FCM notifi… (#453)
* allow sending data ONLY notifications for FCM notifications ran into a little issue here need this tweak to be able to send and process background notifications on android If you want to process android notifications in the BACKGROUND, they need to be data notifications, as in not contain the "notification" key in the payload. This little change achieves it, because omitempty on structs for the JSON encoding does not work on sub-structs except if it is a pointer to it and set to nil. Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
		
							parent
							
								
									cd83da717d
								
							
						
					
					
						commit
						5cb40cf5b4
					
				| 
						 | 
					@ -70,15 +70,15 @@ type PushNotification struct {
 | 
				
			||||||
	Retry            int         `json:"retry,omitempty"`
 | 
						Retry            int         `json:"retry,omitempty"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Android
 | 
						// Android
 | 
				
			||||||
	APIKey                string           `json:"api_key,omitempty"`
 | 
						APIKey                string            `json:"api_key,omitempty"`
 | 
				
			||||||
	To                    string           `json:"to,omitempty"`
 | 
						To                    string            `json:"to,omitempty"`
 | 
				
			||||||
	CollapseKey           string           `json:"collapse_key,omitempty"`
 | 
						CollapseKey           string            `json:"collapse_key,omitempty"`
 | 
				
			||||||
	DelayWhileIdle        bool             `json:"delay_while_idle,omitempty"`
 | 
						DelayWhileIdle        bool              `json:"delay_while_idle,omitempty"`
 | 
				
			||||||
	TimeToLive            *uint            `json:"time_to_live,omitempty"`
 | 
						TimeToLive            *uint             `json:"time_to_live,omitempty"`
 | 
				
			||||||
	RestrictedPackageName string           `json:"restricted_package_name,omitempty"`
 | 
						RestrictedPackageName string            `json:"restricted_package_name,omitempty"`
 | 
				
			||||||
	DryRun                bool             `json:"dry_run,omitempty"`
 | 
						DryRun                bool              `json:"dry_run,omitempty"`
 | 
				
			||||||
	Condition             string           `json:"condition,omitempty"`
 | 
						Condition             string            `json:"condition,omitempty"`
 | 
				
			||||||
	Notification          fcm.Notification `json:"notification,omitempty"`
 | 
						Notification          *fcm.Notification `json:"notification,omitempty"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// iOS
 | 
						// iOS
 | 
				
			||||||
	Expiration  *int64   `json:"expiration,omitempty"`
 | 
						Expiration  *int64   `json:"expiration,omitempty"`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -60,19 +60,21 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	notification.Notification = &req.Notification
 | 
						notification.Notification = req.Notification
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Set request message if body is empty
 | 
						if req.Notification != nil {
 | 
				
			||||||
	if len(req.Message) > 0 {
 | 
							// Set request message if body is empty
 | 
				
			||||||
		notification.Notification.Body = req.Message
 | 
							if len(req.Message) > 0 {
 | 
				
			||||||
	}
 | 
								notification.Notification.Body = req.Message
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(req.Title) > 0 {
 | 
							if len(req.Title) > 0 {
 | 
				
			||||||
		notification.Notification.Title = req.Title
 | 
								notification.Notification.Title = req.Title
 | 
				
			||||||
	}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if v, ok := req.Sound.(string); ok && len(v) > 0 {
 | 
							if v, ok := req.Sound.(string); ok && len(v) > 0 {
 | 
				
			||||||
		notification.Notification.Sound = v
 | 
								notification.Notification.Sound = v
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return notification
 | 
						return notification
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -235,9 +235,10 @@ func TestAndroidNotificationStructure(t *testing.T) {
 | 
				
			||||||
			"a": "1",
 | 
								"a": "1",
 | 
				
			||||||
			"b": 2,
 | 
								"b": 2,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		Notification: fcm.Notification{
 | 
							Notification: &fcm.Notification{
 | 
				
			||||||
			Color: test,
 | 
								Color: test,
 | 
				
			||||||
			Tag:   test,
 | 
								Tag:   test,
 | 
				
			||||||
 | 
								Body:  "",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -263,6 +264,9 @@ func TestAndroidNotificationStructure(t *testing.T) {
 | 
				
			||||||
	req = PushNotification{
 | 
						req = PushNotification{
 | 
				
			||||||
		Tokens: []string{"a", "b"},
 | 
							Tokens: []string{"a", "b"},
 | 
				
			||||||
		To:     test,
 | 
							To:     test,
 | 
				
			||||||
 | 
							Notification: &fcm.Notification{
 | 
				
			||||||
 | 
								Body: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	notification = GetAndroidNotification(req)
 | 
						notification = GetAndroidNotification(req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue