appleboy / gorush

A push notification server written in Go (Golang).
MIT License
8.09k stars 846 forks source link

sending notifications to Huawei does not work via gRPC #765

Open vanohaker opened 7 months ago

vanohaker commented 7 months ago

My code:

func (g *GrpcGorush) SendToHuaweiAndroid(body Notification, index int, uuids []string) {
    request, err := g.gorushClient.Send(context.Background(), &proto.NotificationRequest{
        Platform: 3,
        Tokens:   uuids,
        // Message:     body.Annotations,
        // Title: body.Title,
        // Priority: proto.NotificationRequest_NORMAL,
        // Image:       body.ImageMedia,
        Data: &structpb.Struct{
            Fields: map[string]*structpb.Value{
                "title": {
                    Kind: &structpb.Value_StringValue{StringValue: body.Title},
                },
                "href": {
                    Kind: &structpb.Value_StringValue{StringValue: body.NotifiHref},
                },
                "image": {
                    Kind: &structpb.Value_StringValue{StringValue: body.ImageMedia},
                },
            },
        },
    })
    if err != nil {
        g.logger.Info().Int("Chunk", index).Err(err)
    }
    if request != nil {
        g.logger.Info().Int("Chunk", index).Msgf("Success: %t", request.Success)
        g.logger.Info().Int("Chunk", index).Msgf("Count: %d", request.Counts)
    }
}

Server log:

{"level":"debug","msg":"Start push notification for Huawei","time":"2024-04-26T10:58:22Z"}
{"level":"debug","msg":"Default message is {\"validate_only\":false,\"message\":{\"android\":{\"urgency\":\"NORMAL\",\"ttl\":\"86400s\"},\"token\":[\"IQAAAACy0sAbAACa6SbyCxYQWR2iMqGJMv....\"]}}","time":"2024-04-26T10:58:22Z"}
{"level":"debug","msg":"Huawei Send Notification is failed! Code: 80100003","time":"2024-04-26T10:58:22Z"}

If I send a message via web api then everything is sent correctly

curl -X POST -H 'Content-Type: application/json' https://push.dev/api/push -d @payload_huawei.json

payload_huawei.json

{
  "notifications": [
    {
      "tokens": ["IQAAAACy0sAbAACa6SbyCxYQWR2iMqGJMv..."],
      "platform": 3,
      "huawei_data": "{'title': 'title text', 'href': 'app_name://n/2823280/?from=push', 'image': 'https://host/home/n/2024/20240419/a.jpg'}"
    }
  ]
}

Server log:

{"level":"debug","msg":"Start push notification for Huawei","time":"2024-04-26T11:04:20Z"}
{"level":"debug","msg":"Default message is {\"validate_only\":false,\"message\":{\"data\":\"{'title': 'title text', 'href': 'app_name://n/2823280/?from=push', 'image': 'https://host/home/n/2024/20240419/a.jpg'}\",\"android\":{\"urgency\":\"NORMAL\",\"ttl\":\"86400s\"},\"token\":[\"IQAAAACy0sAbAACa6SbyCxYQWR2iMqGJMv...\"]}}","time":"2024-04-26T11:04:20Z"}
{"level":"debug","msg":"Huwaei Send Notification is completed successfully!","time":"2024-04-26T11:04:20Z"}

The NotificationRequest structure does not have a hoawei_data field. I can pass *structpb.Struct as Data, and not string as in huawei_data. How to correctly pass a string to Data or is this some kind of bug in the implementation of the rpc server?

appleboy commented 5 months ago

I will take it.