github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/push_notification.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"strings"
    10  )
    11  
    12  const (
    13  	PUSH_NOTIFY_APPLE                = "apple"
    14  	PUSH_NOTIFY_ANDROID              = "android"
    15  	PUSH_NOTIFY_APPLE_REACT_NATIVE   = "apple_rn"
    16  	PUSH_NOTIFY_ANDROID_REACT_NATIVE = "android_rn"
    17  
    18  	PUSH_TYPE_MESSAGE = "message"
    19  	PUSH_TYPE_CLEAR   = "clear"
    20  
    21  	// The category is set to handle a set of interactive Actions
    22  	// with the push notifications
    23  	CATEGORY_CAN_REPLY = "CAN_REPLY"
    24  
    25  	MHPNS = "https://push.mattermost.com"
    26  )
    27  
    28  type PushNotification struct {
    29  	Platform         string `json:"platform"`
    30  	ServerId         string `json:"server_id"`
    31  	DeviceId         string `json:"device_id"`
    32  	Category         string `json:"category"`
    33  	Sound            string `json:"sound"`
    34  	Message          string `json:"message"`
    35  	Badge            int    `json:"badge"`
    36  	ContentAvailable int    `json:"cont_ava"`
    37  	TeamId           string `json:"team_id"`
    38  	ChannelId        string `json:"channel_id"`
    39  	PostId           string `json:"post_id"`
    40  	RootId           string `json:"root_id"`
    41  	ChannelName      string `json:"channel_name"`
    42  	Type             string `json:"type"`
    43  	SenderId         string `json:"sender_id"`
    44  	OverrideUsername string `json:"override_username"`
    45  	OverrideIconUrl  string `json:"override_icon_url"`
    46  	FromWebhook      string `json:"from_webhook"`
    47  }
    48  
    49  func (me *PushNotification) ToJson() string {
    50  	b, _ := json.Marshal(me)
    51  	return string(b)
    52  }
    53  
    54  func (me *PushNotification) SetDeviceIdAndPlatform(deviceId string) {
    55  
    56  	index := strings.Index(deviceId, ":")
    57  
    58  	if index > -1 {
    59  		me.Platform = deviceId[:index]
    60  		me.DeviceId = deviceId[index+1:]
    61  	}
    62  }
    63  
    64  func PushNotificationFromJson(data io.Reader) *PushNotification {
    65  	var me *PushNotification
    66  	json.NewDecoder(data).Decode(&me)
    67  	return me
    68  }