Skip to content
Snippets Groups Projects
Commit cb0bfcae authored by Jonah Husson's avatar Jonah Husson
Browse files

Fixes for apns sending

parent 80922f76
No related branches found
No related tags found
2 merge requests!12Release,!7Jonah/alt apns
...@@ -38,7 +38,7 @@ import ( ...@@ -38,7 +38,7 @@ import (
) )
// Function type definitions for the main operations (poll and notify) // Function type definitions for the main operations (poll and notify)
type NotifyFunc func(*pb.NotificationData, ApnsSender, *messaging.Client, *firebase.FirebaseComm, *storage.Storage) error type NotifyFunc func(*pb.NotificationData, ApnsSender, *messaging.Client, *firebase.FirebaseComm, *storage.Storage, string) error
type ApnsSender interface { type ApnsSender interface {
//Send(token string, p apns.Payload, opts ...apns.SendOption) (*apns.Response, error) //Send(token string, p apns.Payload, opts ...apns.SendOption) (*apns.Response, error)
Push(n *apns2.Notification) (*apns2.Response, error) Push(n *apns2.Notification) (*apns2.Response, error)
...@@ -69,6 +69,7 @@ type Impl struct { ...@@ -69,6 +69,7 @@ type Impl struct {
fcm *messaging.Client fcm *messaging.Client
apnsClient *apns2.Client apnsClient *apns2.Client
receivedNdf *uint32 receivedNdf *uint32
params *Params
ndfStopper Stopper ndfStopper Stopper
} }
...@@ -110,6 +111,7 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) { ...@@ -110,6 +111,7 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
notifyFunc: notifyUser, notifyFunc: notifyUser,
fcm: app, fcm: app,
receivedNdf: &receivedNdf, receivedNdf: &receivedNdf,
params: &params,
} }
if params.APNS.KeyPath == "" { if params.APNS.KeyPath == "" {
...@@ -119,7 +121,7 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) { ...@@ -119,7 +121,7 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
return nil, errors.WithMessagef(err, "APNS not properly configured: %+v", params.APNS) return nil, errors.WithMessagef(err, "APNS not properly configured: %+v", params.APNS)
} }
authKey, err := apnstoken.AuthKeyFromFile("params.APNS.KeyPath") authKey, err := apnstoken.AuthKeyFromFile(params.APNS.KeyPath)
if err != nil { if err != nil {
log.Fatal("token error:", err) log.Fatal("token error:", err)
} }
...@@ -131,6 +133,12 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) { ...@@ -131,6 +133,12 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
TeamID: params.APNS.Issuer, TeamID: params.APNS.Issuer,
} }
apnsClient := apns2.NewTokenClient(token) apnsClient := apns2.NewTokenClient(token)
if params.APNS.Dev {
jww.INFO.Printf("Running with dev apns gateway")
apnsClient.Development()
} else {
apnsClient.Production()
}
impl.apnsClient = apnsClient impl.apnsClient = apnsClient
} }
...@@ -170,7 +178,7 @@ func NewImplementation(instance *Impl) *notificationBot.Implementation { ...@@ -170,7 +178,7 @@ func NewImplementation(instance *Impl) *notificationBot.Implementation {
// NotifyUser accepts a UID and service key file path. // NotifyUser accepts a UID and service key file path.
// It handles the logic involved in retrieving a user's token and sending the notification // It handles the logic involved in retrieving a user's token and sending the notification
func notifyUser(data *pb.NotificationData, apnsClient ApnsSender, fcm *messaging.Client, fc *firebase.FirebaseComm, db *storage.Storage) error { func notifyUser(data *pb.NotificationData, apnsClient ApnsSender, fcm *messaging.Client, fc *firebase.FirebaseComm, db *storage.Storage, topic string) error {
elist, err := db.GetEphemeral(data.EphemeralID) elist, err := db.GetEphemeral(data.EphemeralID)
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
...@@ -201,6 +209,7 @@ func notifyUser(data *pb.NotificationData, apnsClient ApnsSender, fcm *messaging ...@@ -201,6 +209,7 @@ func notifyUser(data *pb.NotificationData, apnsClient ApnsSender, fcm *messaging
Priority: apns2.PriorityHigh, Priority: apns2.PriorityHigh,
Payload: notifPayload, Payload: notifPayload,
PushType: apns2.PushTypeAlert, PushType: apns2.PushTypeAlert,
Topic: topic,
} }
resp, err := apnsClient.Push(notif) resp, err := apnsClient.Push(notif)
//resp, err := apnsClient.Send(u.Token, apns.Payload{ //resp, err := apnsClient.Send(u.Token, apns.Payload{
...@@ -349,7 +358,7 @@ func (nb *Impl) ReceiveNotificationBatch(notifBatch *pb.NotificationBatch, auth ...@@ -349,7 +358,7 @@ func (nb *Impl) ReceiveNotificationBatch(notifBatch *pb.NotificationBatch, auth
fbComm := firebase.NewFirebaseComm() fbComm := firebase.NewFirebaseComm()
for _, notifData := range notifBatch.GetNotifications() { for _, notifData := range notifBatch.GetNotifications() {
err := nb.notifyFunc(notifData, nb.apnsClient, nb.fcm, fbComm, nb.Storage) err := nb.notifyFunc(notifData, nb.apnsClient, nb.fcm, fbComm, nb.Storage, nb.params.APNS.BundleID)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -77,7 +77,7 @@ func TestNotifyUser(t *testing.T) { ...@@ -77,7 +77,7 @@ func TestNotifyUser(t *testing.T) {
EphemeralID: eph.EphemeralId, EphemeralID: eph.EphemeralId,
IdentityFP: nil, IdentityFP: nil,
MessageHash: nil, MessageHash: nil,
}, &MockApns{}, nil, fcBadSend, s) }, &MockApns{}, nil, fcBadSend, s, "")
if err == nil { if err == nil {
t.Errorf("Should have returned an error") t.Errorf("Should have returned an error")
} }
...@@ -86,7 +86,7 @@ func TestNotifyUser(t *testing.T) { ...@@ -86,7 +86,7 @@ func TestNotifyUser(t *testing.T) {
EphemeralID: eph.EphemeralId, EphemeralID: eph.EphemeralId,
IdentityFP: nil, IdentityFP: nil,
MessageHash: nil, MessageHash: nil,
}, &MockApns{}, nil, fc, s) }, &MockApns{}, nil, fc, s, "")
if err != nil { if err != nil {
t.Errorf("Failed to notify user properly") t.Errorf("Failed to notify user properly")
} }
...@@ -297,7 +297,7 @@ func TestImpl_UnregisterForNotifications(t *testing.T) { ...@@ -297,7 +297,7 @@ func TestImpl_UnregisterForNotifications(t *testing.T) {
func TestImpl_ReceiveNotificationBatch(t *testing.T) { func TestImpl_ReceiveNotificationBatch(t *testing.T) {
impl := getNewImpl() impl := getNewImpl()
dataChan := make(chan *pb.NotificationData) dataChan := make(chan *pb.NotificationData)
impl.notifyFunc = func(data *pb.NotificationData, apns ApnsSender, f *messaging.Client, fc *firebase.FirebaseComm, s *storage.Storage) error { impl.notifyFunc = func(data *pb.NotificationData, apns ApnsSender, f *messaging.Client, fc *firebase.FirebaseComm, s *storage.Storage, topic string) error {
go func() { dataChan <- data }() go func() { dataChan <- data }()
return nil return nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment