2017-06-28 25 views
0

当我试图在golang App Engine中使用onesignal enviorment实现推送通知时,但iam出错“http.DefaultTransport和http.DefaultClient不是在App Engine中可用”。这是我的代码,https://onesignal.com/api/v1//notifications:http.DefaultTransport和http.DefaultClient在App Engine中不可用

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

使用HTTP AppEngine上,你必须使用网址抓取。

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

即使用不支持的AppEngine包。

+0

有什么办法可以使软件包应用引擎兼容?该包用于使用oneignal发送推送通知 –

+1

@farsanapb应通过上下文传递上下文:'c:= appengine.NewContext(r)' 这个'r'是请求对象。所以为NewXXX添加context.Context似乎对我来说是个好主意。 – mattn

1

类似的问题已经被Robby Colvin in this blog

解决它说明了如何在这样的条件下第三方包运行。希望这会有所帮助。