mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
insuring single push sent per device
This commit is contained in:
parent
047f524824
commit
3bfce5e40c
@ -52,36 +52,43 @@ func SendPushEvent(account store.Account, event string) {
|
||||
ErrMsg(err);
|
||||
return
|
||||
}
|
||||
|
||||
tokens := make(map[string]bool)
|
||||
for rows.Next() {
|
||||
var pushToken string
|
||||
var messageTitle string
|
||||
var messageBody string
|
||||
|
||||
rows.Scan(&pushToken, &messageTitle, &messageBody)
|
||||
|
||||
url := "https://fcm.googleapis.com/fcm/send"
|
||||
payload := Payload{ Title: messageTitle, Body: messageBody, Sound: "default" };
|
||||
message := Message{ Notification: payload, To: pushToken };
|
||||
if _, exists := tokens[pushToken]; !exists {
|
||||
tokens[pushToken] = true;
|
||||
|
||||
body, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
ErrMsg(err)
|
||||
continue
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
ErrMsg(err)
|
||||
continue
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
req.Header.Set("Authorization", "key=AAAAkgDXt8c:APA91bEjH67QpUWU6uAfCIXLqm0kf6AdPNVICZPCcWbmgW9NGYIErAxMDTy4LEbe4ik93Ho4Z-AJNIhr6nXXKC9qKmyKkkYHJWAEVH47_FXBQV6rsoi9ZB_oiuV66XKKAy1V40GmvfaX")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
ErrMsg(err)
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
ErrMsg(errors.New("failed to push notification"));
|
||||
url := "https://fcm.googleapis.com/fcm/send"
|
||||
payload := Payload{ Title: messageTitle, Body: messageBody, Sound: "default" };
|
||||
message := Message{ Notification: payload, To: pushToken };
|
||||
|
||||
body, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
ErrMsg(err)
|
||||
continue
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
ErrMsg(err)
|
||||
continue
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
req.Header.Set("Authorization", "key=AAAAkgDXt8c:APA91bEjH67QpUWU6uAfCIXLqm0kf6AdPNVICZPCcWbmgW9NGYIErAxMDTy4LEbe4ik93Ho4Z-AJNIhr6nXXKC9qKmyKkkYHJWAEVH47_FXBQV6rsoi9ZB_oiuV66XKKAy1V40GmvfaX")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
ErrMsg(err)
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
ErrMsg(errors.New("failed to push notification"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user