Skip to content

Commit

Permalink
Merge pull request #5 from mattermost/plt-3462
Browse files Browse the repository at this point in the history
PLT-3462 Added the ability to clear notifications
  • Loading branch information
jwilander authored Aug 24, 2016
2 parents f38b7a9 + dd46bcd commit 865a20d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 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
23 changes: 17 additions & 6 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)

if msg.Type != PUSH_TYPE_CLEAR {
payload.Alert = emoji.Sprint(msg.Message)
payload.Badge = msg.Badge
payload.Category = msg.Category
payload.Sound = "default"
}

payload.Badge = msg.Badge
payload.Category = msg.Category
payload.Sound = "default"

pn := apns.NewPushNotification()
pn.DeviceToken = msg.DeviceId
Expand Down

0 comments on commit 865a20d

Please sign in to comment.