From 3538c3a0d7f6f7c6bafb237fab1f4d348833e128 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Thu, 18 Aug 2016 10:32:39 -0400 Subject: [PATCH] Added the ability to clear notifications --- server/push_notification.go | 3 +++ server/server.go | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/server/push_notification.go b/server/push_notification.go index fb7028aa..f6d454a6 100644 --- a/server/push_notification.go +++ b/server/push_notification.go @@ -11,6 +11,8 @@ import ( const ( PUSH_NOTIFY_APPLE = "apple" PUSH_NOTIFY_ANDROID = "android" + PUSH_TYPE_MESSAGE = "message" + PUSH_TYPE_CLEAR = "clear" ) type PushNotification struct { @@ -24,6 +26,7 @@ type PushNotification struct { ContentAvailable int `json:"cont_ava"` ChannelId string `json:"channel_id"` ChannelName string `json:"channel_name"` + Type string `json:"type"` } func (me *PushNotification) ToJson() string { diff --git a/server/server.go b/server/server.go index 8a3b8047..b971aba0 100644 --- a/server/server.go +++ b/server/server.go @@ -90,10 +90,16 @@ func handleSendNotification(w http.ResponseWriter, r *http.Request) { } func sendAndroidNotification(msg *PushNotification) { - data := map[string]interface{}{"message": emoji.Sprint(msg.Message), "channel_id": msg.ChannelId, "channel_name": msg.ChannelName} - regIDs := []string{msg.DeviceId} + var data map[string]interface{} + if msg.Type == PUSH_TYPE_CLEAR { + data = map[string]interface{}{"type": PUSH_TYPE_CLEAR, "channel_id": msg.ChannelId} + } else { + data = map[string]interface{}{"type": PUSH_TYPE_MESSAGE, "message": emoji.Sprint(msg.Message), "channel_id": msg.ChannelId, "channel_name": msg.ChannelName} + } + regIDs := []string{msg.DeviceId} gcmMsg := gcm.NewMessage(data, regIDs...) + sender := &gcm.Sender{ApiKey: CfgPP.AndroidApiKey} LogInfo("Sending android push notification") @@ -105,16 +111,21 @@ func sendAndroidNotification(msg *PushNotification) { } if resp.Failure > 0 { - LogError(fmt.Sprintf("Android reponse: %v", resp)) + LogError(fmt.Sprintf("Android response: %v", resp)) } } func sendAppleNotification(msg *PushNotification) { payload := apns.NewPayload() - payload.Alert = emoji.Sprint(msg.Message) - payload.Badge = msg.Badge - payload.Category = msg.Category - payload.Sound = "default" + + if msg.Type == PUSH_TYPE_CLEAR { + payload.Badge = 0 + } else { + payload.Alert = emoji.Sprint(msg.Message) + payload.Badge = msg.Badge + payload.Category = msg.Category + payload.Sound = "default" + } pn := apns.NewPushNotification() pn.DeviceToken = msg.DeviceId