This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios.go
67 lines (57 loc) · 1.55 KB
/
ios.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pushover
import (
"context"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"google.golang.org/api/option"
"github.com/nam-truong-le/lambda-utils-go/v4/pkg/aws/secretsmanager"
"github.com/nam-truong-le/lambda-utils-go/v4/pkg/logger"
)
func sendToIOSApp(ctx context.Context, title, body string) error {
log := logger.FromContext(ctx)
credentials, err := secretsmanager.GetSecret(ctx, "firebaseServiceAccount")
if err != nil {
log.Errorf("error getting secret: %v\n", err)
return err
}
// Initialize the Firebase app
opt := option.WithCredentialsJSON([]byte(credentials))
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
log.Errorf("error initializing app: %v\n", err)
return err
}
// Initialize Firebase Cloud Messaging client
client, err := app.Messaging(ctx)
if err != nil {
log.Errorf("error getting Messaging client: %v\n", err)
return err
}
// Topic to which you want to send a notification
topic := "newOrders"
// Create the message to be sent
message := &messaging.Message{
Topic: topic,
Notification: &messaging.Notification{
Title: title,
Body: body,
},
Data: map[string]string{},
APNS: &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
Sound: "default",
},
},
},
}
// Send the message
response, err := client.Send(ctx, message)
if err != nil {
log.Errorf("error sending message: %v\n", err)
return err
}
// Response is a message ID string
log.Infof("Successfully sent message: %s\n", response)
return nil
}