✨ Apple push notification services
This commit is contained in:
		
							
								
								
									
										25
									
								
								pkg/services/external_apns.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								pkg/services/external_apns.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/sideshow/apns2"
 | 
			
		||||
	"github.com/sideshow/apns2/token"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ExtAPNS is Apple Notification Services client
 | 
			
		||||
var ExtAPNS *apns2.Client
 | 
			
		||||
 | 
			
		||||
func SetupAPNS() error {
 | 
			
		||||
	authKey, err := token.AuthKeyFromFile(viper.GetString("apns_credentials"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ExtAPNS = apns2.NewTokenClient(&token.Token{
 | 
			
		||||
		AuthKey: authKey,
 | 
			
		||||
		KeyID:   viper.GetString("apns_credentials_key"),
 | 
			
		||||
		TeamID:  viper.GetString("apns_credentials_team"),
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
@@ -7,6 +7,7 @@ import (
 | 
			
		||||
	"google.golang.org/api/option"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ExtFire is the firebase app client
 | 
			
		||||
var ExtFire *firebase.App
 | 
			
		||||
 | 
			
		||||
func SetupFirebase() error {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,11 +3,12 @@ package services
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"firebase.google.com/go/messaging"
 | 
			
		||||
	"reflect"
 | 
			
		||||
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/database"
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/models"
 | 
			
		||||
	"github.com/rs/zerolog/log"
 | 
			
		||||
	"github.com/sideshow/apns2"
 | 
			
		||||
	payload2 "github.com/sideshow/apns2/payload"
 | 
			
		||||
	"reflect"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (models.NotificationSubscriber, error) {
 | 
			
		||||
@@ -105,6 +106,27 @@ func PushNotification(notification models.Notification) error {
 | 
			
		||||
						Msg("Notified subscriber via FCM.")
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		case models.NotifySubscriberAPNs:
 | 
			
		||||
			if ExtAPNS != nil {
 | 
			
		||||
				data, err := payload2.
 | 
			
		||||
					NewPayload().
 | 
			
		||||
					AlertTitle(notification.Subject).
 | 
			
		||||
					AlertBody(notification.Content).
 | 
			
		||||
					MarshalJSON()
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Warn().Err(err).Msg("An error occurred when preparing to notify subscriber via APNs...")
 | 
			
		||||
				}
 | 
			
		||||
				payload := &apns2.Notification{
 | 
			
		||||
					DeviceToken: subscriber.DeviceToken,
 | 
			
		||||
					Topic:       "dev.solsynth.solian.UniversalNotification",
 | 
			
		||||
					Payload:     data,
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				_, err = ExtAPNS.Push(payload)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Warn().Err(err).Msg("An error occurred when notify subscriber via APNs...")
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user