Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Firebase Cloud Messaging #62

Merged
merged 25 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f6208a4
initial commit for adding Firebase Cloud Messaging
chemidy Dec 26, 2017
0759106
add validator
chemidy Dec 26, 2017
049c425
Merge remote-tracking branch 'upstream/dev' into fcm
chemidy Dec 26, 2017
a0ba55c
use http const in messaging test
chemidy Dec 26, 2017
c40516b
add client version header for stats
chemidy Dec 26, 2017
c91fb40
init integration test
chemidy Dec 29, 2017
d312b7c
add integration test (validated on IOS today)
Jan 2, 2018
0e83458
add comment with URL to enable Firebase Cloud Messaging API
Jan 2, 2018
94cd35e
fix broken test
chemidy Jan 2, 2018
6d76105
add integration tests
chemidy Jan 2, 2018
4e24472
Merge branch 'dev' into fcm
chemidy Jan 2, 2018
aee449c
accept a Message instead of RequestMessage + and rename method + send…
chemidy Jan 2, 2018
4b37eee
Merge branch 'dev' into fcm
chemidy Jan 2, 2018
642de9f
Merge branch 'dev' into fcm
chemidy Jan 6, 2018
3c6dbcf
Merge remote-tracking branch 'upstream/dev' into fcm
chemidy Jan 9, 2018
746bfb7
update fcm url
chemidy Jan 9, 2018
8a58a72
rollback url endpoint
chemidy Jan 9, 2018
78303e6
Merge remote-tracking branch 'upstream/dev' into fcm
Jan 18, 2018
c72d90f
fix http constants, change responseMessage visibility, change map[str…
Jan 18, 2018
7091e4b
fix http constants
Jan 18, 2018
06195be
fix integration tests
Jan 18, 2018
8b2f6bf
fix APNS naming
chemidy Jan 25, 2018
cb9e342
Merge branch 'dev' into fcm
chemidy Jan 25, 2018
0802b84
Merge branch 'dev' into fcm
chemidy Jan 30, 2018
24bdea0
add validators
chemidy Jan 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"firebase.google.com/go/auth"
"firebase.google.com/go/iid"
"firebase.google.com/go/internal"
"firebase.google.com/go/messaging"
"firebase.google.com/go/storage"

"os"
Expand All @@ -42,6 +43,7 @@ var firebaseScopes = []string{
"https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/identitytoolkit",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.messaging",
}

// Version of the Firebase Go Admin SDK.
Expand Down Expand Up @@ -99,6 +101,16 @@ func (a *App) InstanceID(ctx context.Context) (*iid.Client, error) {
return iid.NewClient(ctx, conf)
}

// Messaging returns an instance of messaging.Client.
func (a *App) Messaging(ctx context.Context) (*messaging.Client, error) {
conf := &internal.MessagingConfig{
ProjectID: a.projectID,
Opts: a.opts,
Version: Version,
}
return messaging.NewClient(ctx, conf)
}

// NewApp creates a new App from the provided config and client options.
//
// If the client options contain a valid credential (a service account file, a refresh token file or an
Expand Down
12 changes: 12 additions & 0 deletions firebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ func TestInstanceID(t *testing.T) {
}
}

func TestMessaging(t *testing.T) {
ctx := context.Background()
app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/service_account.json"))
if err != nil {
t.Fatal(err)
}

if c, err := app.Messaging(ctx); c == nil || err != nil {
t.Errorf("Messaging() = (%v, %v); want (iid, nil)", c, err)
}
}

func TestCustomTokenSource(t *testing.T) {
ctx := context.Background()
ts := &testTokenSource{AccessToken: "mock-token-from-custom"}
Expand Down
338 changes: 338 additions & 0 deletions integration/messaging/messaging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,338 @@
package messaging

import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"testing"

"firebase.google.com/go/integration/internal"
"firebase.google.com/go/messaging"
)

var projectID string
var client *messaging.Client

var testFixtures = struct {
token string
topic string
condition string
}{}

// Enable API before testing
// https://console.developers.google.com/apis/library/fcm.googleapis.com/?project=
func TestMain(m *testing.M) {
flag.Parse()
if testing.Short() {
log.Println("skipping Messaging integration tests in short mode.")
return
}

token, err := ioutil.ReadFile(internal.Resource("integration_token.txt"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
log.Fatalln(err)
}
testFixtures.token = string(token)

topic, err := ioutil.ReadFile(internal.Resource("integration_topic.txt"))
if err != nil {
log.Fatalln(err)
}
testFixtures.topic = string(topic)

condition, err := ioutil.ReadFile(internal.Resource("integration_condition.txt"))
if err != nil {
log.Fatalln(err)
}
testFixtures.condition = string(condition)

ctx := context.Background()
app, err := internal.NewTestApp(ctx)
if err != nil {
log.Fatalln(err)
}

projectID, err = internal.ProjectID()
if err != nil {
log.Fatalln(err)
}

client, err = app.Messaging(ctx)

if err != nil {
log.Fatalln(err)
}
os.Exit(m.Run())
}

func TestSendMessageInvalidToken(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: "INVALID_TOKEN",
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
},
}
_, err := client.SendMessage(ctx, msg)

if err == nil {
log.Fatal(err)
}
}

func TestSendMessageValidateOnly(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
ValidateOnly: true,
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name != fmt.Sprintf("projects/%s/messages/fake_message_id", projectID) {
t.Errorf("Name : %s; want : projects/%s/messages/fake_message_id", resp.Name, projectID)
}
}

func TestSendMessageToToken(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendMessageToTopic(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Topic: testFixtures.topic,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendMessageToCondition(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Condition: testFixtures.condition,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendNotificationMessage(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendDataMessage(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Data: map[string]interface{}{
"private_key": "foo",
"client_email": "bar@test.com",
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendAndroidNotificationMessage(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
Android: messaging.AndroidConfig{
CollapseKey: "Collapse",
Priority: "HIGH",
TTL: "3.5s",
Notification: messaging.AndroidNotification{
Title: "Android Title",
Body: "Android body",
},
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendAndroidDataMessage(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
Android: messaging.AndroidConfig{
CollapseKey: "Collapse",
Priority: "HIGH",
TTL: "3.5s",
Data: map[string]interface{}{
"private_key": "foo",
"client_email": "bar@test.com",
},
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendApnsNotificationMessage(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
Apns: messaging.ApnsConfig{
Payload: map[string]interface{}{
"title": "APNS Title ",
"body": "APNS bodym",
},
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}

func TestSendApnsDataMessage(t *testing.T) {
ctx := context.Background()
msg := &messaging.RequestMessage{
Message: messaging.Message{
Token: testFixtures.token,
Notification: messaging.Notification{
Title: "My Title",
Body: "This is a Notification",
},
Apns: messaging.ApnsConfig{
Headers: map[string]interface{}{
"private_key": "foo",
"client_email": "bar@test.com",
},
},
},
}
resp, err := client.SendMessage(ctx, msg)

if err != nil {
log.Fatal(err)
}

if resp.Name == "" {
t.Errorf("Name : %s; want : projects/%s/messages/#id#", resp.Name, projectID)
}
}
Loading