appleboy / gorush

A push notification server written in Go (Golang).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

structpb.Struct serialisation in proto Send method

jvallesm opened this issue · comments

Hi 👋

#638, aimed to fix #418, introduced a change on how structpb.Struct objects in the gRPC Send request are serialised.

Context

Some extra data is sent in the notification request so the mobile knows the notification source in order to provide analytics and can display a popup within the app:

	req := &gorushapi.NotificationRequest{
		ID:       notification.ID,
		Platform: firebaseProvider,
		Tokens:   []string{token},
		Priority: gorushapi.NotificationRequest_HIGH,
	}

	data := map[string]interface{}{
		"source": "poc.pushnotif",
		"popup": map[string]interface{}{
			"title": notification.Popup.Title,
			"body":  notification.Popup.Body,
		},
	}

	pbdata, err := structpb.NewStruct(data)
	if err != nil {
		return fmt.Errorf("couldn't convert notification data to pbstruct: %w", err)
	}

	req.Data = pbdata

In Gorush v1.14.0, this is how Data is serialised:

{
  "popup": {
    "body": "🥘 How about a visit to your favourite restaurant?",
    "title": "Time to eat!"
  },
  "source": "poc.pushnotif"
}

In Gorush v1.15.0, however, this is what we get:

{
  "popup": "map[body:🥘 How about a ride to your favourite restaurant? title:Time to eat!]",
  "source": "poc.pushnotif"
}

Therefore, after upgrading the backend to use Gorush 1.15.0, mobile can't parse the popup object correctly.

Proposal

Changing this line fixes the behaviour (for the aforementioned example):

		notification.Data = map[string]interface{}{}
		for k, v := range in.Data.AsMap() {
			notification.Data[k] = v
		}

However, I might be lacking context on why fmt.Sprintf("%v", v) was needed, so it might be necessary to check if this change doesn't break the solution for #418.

fixed in #674