https://onesignal.com/api/v1//notifications: http.DefaultTransport and http.DefaultClient are not available in App Engine

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

https://onesignal.com/api/v1//notifications: http.DefaultTransport and http.DefaultClient are not available in App Engine

问题

当我尝试在使用OneSignal环境的Golang App Engine中实现推送通知时,遇到了错误“App Engine中不可用http.DefaultTransport和http.DefaultClient”。这是我的代码:

func (c *PushNotificationController) CreateNotification() {
    client := onesignal.NewClient(nil)
    client.AppKey = "MyAppKey"
    client.UserKey = "MyUserKey"
    notifID := CreateNotifications(client)
    log.Println(notifID)
}

func CreateNotifications(client *onesignal.Client) string {
    playerID := "SamplePlayerId" // 有效的ID
    notificationReq := &onesignal.NotificationRequest{
        AppID:            "MyAppKey",
        Contents:         map[string]string{"en": "English message"},
        IsIOS:            true,
        IncludePlayerIDs: []string{playerID},
    }
    if createRes, res, err := client.Notifications.Create(notificationReq) {
        if err != nil {
            log.Fatal(err)
        }
        return createRes.ID
    }
    // ...
}

希望这可以帮助到你!

英文:

When i tried to implement push notification in golang App Engine using onesignal enviorment.But iam getting error "http.DefaultTransport and http.DefaultClient are not available in App Engine".This is my code,

func (c *PushNotificationController) CreateNotification() {
	client := onesignal.NewClient(nil)
	client.AppKey = "MyAppKey"
	client.UserKey = "MyUserKey"
	notifID := CreateNotifications(client)
	log.Println(notifID)
}

func CreateNotifications(client *onesignal.Client) string {
	playerID := "SamplePlayerId" // valid
	notificationReq := &onesignal.NotificationRequest{
		AppID:            "MyAppKey",
		Contents:         map[string]string{"en": "English message"},
		IsIOS:            true,
		IncludePlayerIDs: []string{playerID},
	}
	if createRes, res, err := client.Notifications.Create(notificationReq){
		if err != nil {
			log.Fatal(err)
		}
		return createRes.ID
	}
    ...
}

答案1

得分: 1

在App Engine上使用HTTP,你需要使用urlfetch。

https://cloud.google.com/appengine/docs/standard/go/urlfetch/reference

也就是说,你使用的包不支持App Engine。

英文:

Use http on appengine, you have to use urlfetch.

https://cloud.google.com/appengine/docs/standard/go/urlfetch/reference

i.e. the package you use doesn't support appengine.

答案2

得分: 1

Robby Colvin在这篇博客中解决了类似的问题。

它解释了如何在这种情况下运行第三方包。希望这对你有帮助。

英文:

A similar issue has been addressed by Robby Colvin in this blog

It explains how to make a third party package run in such conditions. Hoping this will help.

1: https://robbycolvin.com/google-app-engine-http-default-transport/ "Robby Colvin in this blog"

huangapple
  • 本文由 发表于 2017年6月28日 15:26:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/44796124.html
匿名

发表评论

匿名网友

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

确定