// Statup // Copyright (C) 2018. Hunter Long and the project contributors // Written by Hunter Long and the project contributors // // https://github.com/hunterlong/statup // // The licenses for most software and other practical works are designed // to take away your freedom to share and change the works. By contrast, // the GNU General Public License is intended to guarantee your freedom to // share and change all versions of a program--to make sure it remains free // software for all its users. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package notifiers import ( "fmt" "github.com/hunterlong/statup/core/notifier" "github.com/hunterlong/statup/types" "github.com/oliveroneill/exponent-server-sdk-golang/sdk" "strings" "time" ) type mobilePush struct { *notifier.Notification } var mobile = &mobilePush{¬ifier.Notification{ Method: "mobile", Title: "Mobile Notifications", Description: "Receive push notifications on your mobile device using the Statup App.", Author: "Hunter Long", AuthorUrl: "https://github.com/hunterlong", Delay: time.Duration(5 * time.Second), Icon: "fas fa-mobile-alt", Form: []notifier.NotificationForm{{ Type: "text", Title: "Mobile Identifiers", Placeholder: "A list of your mobile push notification ids", DbField: "var1", }}}, } // init the discord notifier func init() { err := notifier.AddNotifier(mobile) if err != nil { panic(err) } } func (u *mobilePush) Select() *notifier.Notification { return u.Notification } // OnFailure will trigger failing service func (u *mobilePush) OnFailure(s *types.Service, f *types.Failure) { msg := &expo.PushMessage{ Body: fmt.Sprintf("Your service '%v' is currently failing! Reason: %v", s.Name, f.Issue), Sound: "default", Title: "Service Offline", Priority: expo.DefaultPriority, } u.AddQueue(s.Id, msg) u.Online = false } // OnSuccess will trigger successful service func (u *mobilePush) OnSuccess(s *types.Service) { if !u.Online { u.ResetUniqueQueue(s.Id) msg := &expo.PushMessage{ Body: fmt.Sprintf("Your service '%v' is back online!", s.Name), Sound: "default", Title: "Service Online", Priority: expo.DefaultPriority, } u.AddQueue(s.Id, msg) } u.Online = true } // OnSave triggers when this notifier has been saved func (u *mobilePush) OnSave() error { msg := &expo.PushMessage{ Body: "This is a test notification", Sound: "default", Title: "Notification Test", Priority: expo.DefaultPriority, } u.AddQueue(0, msg) return nil } // OnTest triggers when this notifier has been saved func (u *mobilePush) OnTest() error { return nil } // Send will send a HTTP Post to the discord API. It accepts type: []byte func (u *mobilePush) Send(msg interface{}) error { pushMessage := msg.(*expo.PushMessage) client := expo.NewPushClient(nil) splitIds := strings.Split(u.Var1, ",") for _, id := range splitIds { pushToken, err := expo.NewExponentPushToken(expo.ExponentPushToken(id)) if err != nil { return err } pushMessage.To = pushToken response, err := client.Publish(pushMessage) if err != nil { return err } if response.ValidateResponse() != nil { fmt.Println(response.PushMessage.To, "failed") } } return nil }