how to send fcm push notification using golang fcm server

huangapple go评论86阅读模式
英文:

how to send fcm push notification using golang fcm server

问题

我正在做Ionic应用程序,使用Cordova fcm插件获取设备令牌。现在我想从Go服务器发送推送通知。如何将Go用作fcm服务器?我需要一些实现示例。

英文:

I'm doing Ionic apps, using Cordova fcm plugin I get the device token. Now I want to send the push notification from go server. How to use go as the fcm server? I need some implementation example.

答案1

得分: 5

请尝试这个,它可以正常工作。

package main

import (
    "fmt"
    "github.com/NaySoftware/go-fcm"
)

const (
     serverKey = "YOUR-KEY"
)

func main() {

  var NP fcm.NotificationPayload 
   NP.Title="你好"
   NP.Body="世界"

    data := map[string]string{
        "msg": "你好世界1",
        "sum": "开心的一天",
    }

  ids := []string{
      "令牌1",
  }


  xds := []string{
      "令牌5",
      "令牌6",
      "令牌7",
  }

    c := fcm.NewFcmClient(serverKey)
    c.NewFcmRegIdsMsg(ids, data)
    c.AppendDevices(xds)
    c.SetNotificationPayload(&NP)
    status, err := c.Send()
    if err == nil {
    status.PrintResults()
    } else {
        fmt.Println(err)
    }

}

尝试一下,它会很好地工作。

英文:
    package main
    
    import (
        "fmt"
        "github.com/NaySoftware/go-fcm"
    )
    
    const (
         serverKey = "YOUR-KEY"
    )
    
    func main() {

  var NP fcm.NotificationPayload 
   NP.Title="hello"
   NP.Body="world"

        data := map[string]string{
            "msg": "Hello World1",
            "sum": "Happy Day",
        }
    
      ids := []string{
          "token1",
      }
    
    
      xds := []string{
          "token5",
          "token6",
          "token7",
      }
    
        c := fcm.NewFcmClient(serverKey)
        c.NewFcmRegIdsMsg(ids, data)
        c.AppendDevices(xds)
        c.SetNotificationPayload(&NP)
        status, err := c.Send()
        if err == nil {
        status.PrintResults()
        } else {
            fmt.Println(err)`enter code here`
        }
    
    }

try this it works cool.

答案2

得分: 3

你的问题比较模糊,你可以通过简单地搜索“fcm golang”或类似的内容来找到答案。基本上,这里有一些库的列表:

https://golanglibs.com/top?q=firebase

看起来它们中的大多数都有一些示例和文档。
一般来说,我会选择最受欢迎的库:

https://github.com/zabawaba99/firego

因为你可以从GitHub的问题和文档中获得一些灵感:

https://godoc.org/gopkg.in/zabawaba99/firego.v1

小提示,如果任何Go库似乎缺乏示例/文档,请尝试在文件夹中运行godocs命令,或者(更快速地)尝试首先在GitHub页面上寻找GoDocs链接。

英文:

Your question is vague and you could have answered it by simply googling fcm golang or something around the line. Basically here's a list of libraries:

https://golanglibs.com/top?q=firebase

It seems like most of them have some examples + docs.
I'd generally speaking go with the most popular:

https://github.com/zabawaba99/firego

Because you can inspire yourself from github issues and the docs are kind of decent:

https://godoc.org/gopkg.in/zabawaba99/firego.v1

Small note, if any go library seems to lack examples/docs try running godocs in the folder or (even faster), try to first look for a GoDocs link on the github page.

huangapple
  • 本文由 发表于 2016年11月19日 18:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/40691612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定