Skip to content

Commit

Permalink
Added the ability to clear notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilander committed Aug 18, 2016
1 parent f38b7a9 commit 3538c3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions server/push_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
25 changes: 18 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 3538c3a

Please sign in to comment.