firebase / firebase-admin-go

Firebase Admin Go SDK
Apache License 2.0
1.15k stars 248 forks source link

[golang]Successfully sent message but not received on Android device. #223

Closed zinwalin closed 5 years ago

zinwalin commented 5 years ago
package main

import (
    "context"
    "fmt"
    "log"
    "time"

    firebase "firebase.google.com/go"
    "firebase.google.com/go/messaging"
    "google.golang.org/api/option"
)

func main() {
    opt := option.WithCredentialsFile("service-account.json")
    app, err := firebase.NewApp(context.Background(), nil, opt)
    if err != nil {
        log.Fatalf("error initializing app: %v\n", err)
    }

    topic := "new_retal_source"

    // See documentation on defining a message payload.
    oneHour := time.Duration(1) * time.Hour
    badge := 42
    message := &messaging.Message{
        Data: map[string]string{
            "score": "850",
            "time":  "2:45",
        },
        Notification: &messaging.Notification{
            Title: "$GOOG up 1.43% on the day",
            Body:  "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
        },
        Android: &messaging.AndroidConfig{
            TTL: &oneHour,
            Notification: &messaging.AndroidNotification{
                Icon:  "stock_ticker_update",
                Color: "#f45342",
            },
        },
        APNS: &messaging.APNSConfig{
            Payload: &messaging.APNSPayload{
                Aps: &messaging.Aps{
                    Badge: &badge,
                },
            },
        },
        Topic: topic,
    }

    ctx := context.Background()
    client, err := app.Messaging(ctx)
    if err != nil {
        log.Fatalf("error getting Messaging client: %v\n", err)
    }

    // Send a message to the device corresponding to the provided
    // registration token.
    response, err := client.Send(ctx, message)
    if err != nil {
        log.Fatalln(err)
    }
    // Response is a message ID string.
    fmt.Println("Successfully sent message:", response)
}

I use the same service-account.json and topic for Python sample, and it works. Anyone use go version to send message?

google-oss-bot commented 5 years ago

This issue does not seem to follow the issue template. Make sure you provide all the required information.

hiranya911 commented 5 years ago

Yes, I've tested it on numerous occasions. What is the error you got?

hiranya911 commented 5 years ago

I'm closing this due to lack of details/follow up. Please provide more details like an error message or a message ID string if this needs to be revisited.