OneSignal Send Push RPC Call Problem

Hi,

I have registered my App on OneSignal. I have added the OneSignal.lua and created pmessage.lua as suggested in the documents. Finally I have created a send_push rpc call and feed it with the needed parameters as described in the documents. The client successfully registers itself to OneSignal and puts the OneSignal Id to the users metadata correctly with the key “os_player_id”. I tried to send a push notification from the OneSignal dashboard and it worked.

But when I call the RPC from the client as below I receive the following error but I couldn’t figured it out what is going on.

I would be grateful if you would help me with this issue.

Mustafa

1-) the error I receive

ApiResponseException: System.Collections.Generic.Dictionary2[System.String,System.Object] Nakama.HttpRequestAdapter+d__6.MoveNext () (at <99ab1ffd513a481fae6067e64cfa51b3>:0) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () (at <567df3e0919241ba98db88bec4c6696f>:0)
Nakama.ApiClient+d__52.MoveNext () (at <99ab1ffd513a481fae6067e64cfa51b3>:0)

2- ) Content of send_push RPC call file I uploaded to Nakama:

local nk = require(“nakama”)
local function send_push(context, payload)
local decoded = nk.json_decode(payload)
local user_ids = decoded.user_ids
local player_ids = {}
local users = nk.users_get_id(user_ids)
for _, u in ipairs(users)
do
table.insert(player_ids, u.metadata.os_player_id)
end
local contents = decoded.contents
local headings = decoded.headings

local included_segments = nil
local filters = nil
local params = {}
onesignal:create_notification(contents, headings, included_segments, filters, player_ids, params)
return payload
end

nk.register_rpc(send_push, “send_push”)
3- The way I call the RPC Call from the client code.

    var payload = new Dictionary<string, object>
    {
        ["user_ids"] = new List<string>() { "22f9e1fd-fae3-46c7-ae92-f0935c9897f7" },
        ["contents"] = new Dictionary<string, string>() { { "en", "MessageExample" } },
        ["headings"] = new Dictionary<string, string>() { { "en", "TitleExample" } }
    };
    await client.RpcAsync(session, "send_push", payload.ToJson());

What version of the Nakama Unity client do you use? Any ApiResponseException should also contain a response code and detailed error message - can you share what these are?

1 Like

Hi

The version of Nakama I use is 2.9.1+31694947

Th entire Exception is below. The part I sent before was the upmost part which repeats itself about where the calls are made following one another.

Thanks,

ApiResponseException: System.Collections.Generic.Dictionary2[System.String,System.Object] Nakama.HttpRequestAdapter+<SendAsync>d__6.MoveNext () (at <99ab1ffd513a481fae6067e64cfa51b3>:0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0) System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () (at <567df3e0919241ba98db88bec4c6696f>:0)
Nakama.ApiClient+d__52.MoveNext () (at <99ab1ffd513a481fae6067e64cfa51b3>:0)
— End of stack trace from previous location where exception was thrown —
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () (at <567df3e0919241ba98db88bec4c6696f>:0)
NakamaClient+d__44.MoveNext () (at Assets/Scripts/NakamaClient.cs:320)
— End of stack trace from previous location where exception was thrown —
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.TaskAwaiter.GetResult () (at <567df3e0919241ba98db88bec4c6696f>:0)
NakamaClient+d__42.MoveNext () (at Assets/Scripts/NakamaClient.cs:252)
— End of stack trace from previous location where exception was thrown —
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <567df3e0919241ba98db88bec4c6696f>:0)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) (at <567df3e0919241ba98db88bec4c6696f>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:150)
UnityEngine.UnitySynchronizationContext:ExecuteTasks() (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:104)

This looks like a Nakama server version. I was asking about your Nakama Unity client library version, not server - can you clarify what version you’re using for this?

1 Like

Sorry for that

The clients version is 2.3.1

Start by updating to 2.4.0 and try it again. ApiResponseException also has a ToString method which is very helpful in getting more insight into the error - try printing that as well.

1 Like

Updated to 2.4.0

Printed the exception in a try catch phrase and the exception is below.

ApiResponseException(StatusCode=500, Message='System.Collections.Generic.Dictionary2[System.String,System.Object]', GrpcStatusCode=13) UnityEngine.Debug:Log(Object) <DenemeAsync2>d__44:MoveNext() (at Assets/Scripts/NakamaClient.cs:327) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetException(Exception)
Nakama.d__52:MoveNext()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetException(Exception)
Nakama.d__6:MoveNext()
UnityEngine.UnitySynchronizationContext:ExecuteTasks() (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:104)

OK so you get a 500 response from the server. Have you checked the server logs for error messages?

1 Like

Interestingly this is how learned that I need to print the error details and heard about 500 response and that I need to check the server logs and be aware of such codes pointing about particular issues. Now I am looking for how to check the logs. I believe I can do that on my own.

I don’t know how to thank you @zyro for taking care of my beginner level questions. I voted and liked your answers if that’s a thing. Other than that, you now have a connection here in Ankara Turkey. If somehow you visit here be my guest.

Just noting my sincere feelings here. I am new in this development business, forums, GitHub and other related matters. And sorry if this is not the place to talk about these things. :wink: Please warn me so that I can delete this.

Below is the related error message from the server logs: the line 23 is: “onesignal:create_notification(contents, headings, included_segments, filters, player_ids, params)”

I have already put the send_push.lua up above. I tried to modify the code you have supplied in the documentation.

Thanks again.

{“level”:“error”,“ts”:“2020-05-30T20:52:50.665Z”,“msg”:“Runtime RPC function caused an error”,“id”:“send_push”,“error”:"/nakama/data/modules/send_push.lua:23: attempt to index a non-table object(nil)\nstack traceback:\n\t/nakama/data/modules/send_push.lua:23: in main chunk\n\t[G]: ?",“stacktrace”:“github.com/heroiclabs/nakama/v2/server.(*RuntimeProviderLua).Rpc\n\tgithub.com/heroiclabs/nakama/v2@/server/runtime_lua.go:1037\ngithub.com/heroiclabs/nakama/v2/server.NewRuntimeProviderLua.func3.1\n\tgithub.com/heroiclabs/nakama/v2@/server/runtime_lua.go:179\ngithub.com/heroiclabs/nakama/v2/server.(*ApiServer).RpcFuncHttp\n\tgithub.com/heroiclabs/nakama/v2@/server/api_rpc.go:160\nnet/http.HandlerFunc.ServeHTTP\n\tnet/http/server.go:2007\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\tgithub.com/gorilla/mux@v1.7.3/mux.go:212\ngo.opencensus.io/plugin/ochttp.(*Handler).ServeHTTP\n\tgo.opencensus.io@v0.22.1/plugin/ochttp/server.go:86\ngithub.com/heroiclabs/nakama/v2/server.decompressHandler.func1\n\tgithub.com/heroiclabs/nakama/v2@/server/api.go:461\nnet/http.HandlerFunc.ServeHTTP\n\tnet/http/server.go:2007\ngithub.com/gorilla/handlers.CompressHandlerLevel.func1\n\tgithub.com/gorilla/handlers@v1.4.2/compress.go:148\nnet/http.HandlerFunc.ServeHTTP\n\tnet/http/server.go:2007\ngithub.com/heroiclabs/nakama/v2/server.StartApiServer.func5\n\tgithub.com/heroiclabs/nakama/v2@/server/api.go:216\nnet/http.HandlerFunc.ServeHTTP\n\tnet/http/server.go:2007\ngithub.com/heroiclabs/nakama/v2/server.StartApiServer.func6\n\tgithub.com/heroiclabs/nakama/v2@/server/api.go:227\nnet/http.HandlerFunc.ServeHTTP\n\tnet/http/server.go:2007\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\tgithub.com/gorilla/mux@v1.7.3/mux.go:212\ngithub.com/gorilla/handlers.(*cors).ServeHTTP\n\tgithub.com/gorilla/handlers@v1.4.2/cors.go:54\nnet/http.serverHandler.ServeHTTP\n\tnet/http/server.go:2802\nnet/http.(*conn).serve\n\tnet/http/server.go:1890”}

@stombee Your problem is the call to onesignal:create_notification. The runtime doesn’t know what onesignal is. You need to either define it locally or import it from another module.

Do you have a onesignal.lua module you’ve also defined?

1 Like

Yes I have that onesignal.lua in the modules and also that pmessage.lua file that is mentioned in the docs.

@stombee I think there’s a typo in the onesignal.lua code with a solution covered in this forum post: Onesignal module has wrong code.

1 Like

I saw that post and fixed that typo already. I created a local onesignal object (that is already created in pmeesage.lua) in the send_push function. I don’t know much about lua and I thought that pmessage.lua will be treated like a static instance.

I just put the entire content of the pmessage.lua into the calling function. I am sure there is a way to refrence the pmessage.lua but couldn’t figure it out. Now the code works but if you could show me the way how to do this properly I will be happy.

Thanks.

@stombee I think at this point you’ll need to share your code snippets as a GitHub gist so that I can understand what needs to be done to improve it.

1 Like

Below is the working code for send_push which sends a push notification to a certain user. In this code I explicitly defined the onesignal object locally in the function definition which is already in the pmessage.lua. I guess there is a way I can refrence pmessage.lua and not need to call the content of it locally in this function, but i dont know how.

> local nk = require("nakama")
> 
> local function send_push(context, payload)
> -- below three lines are the content of pmessage.lua
>     local ONESIGNAL_API_KEY = "xxxxx"
>     local ONESIGNAL_APP_ID = "yyyyy"
>     local onesignal = require("onesignal").new(ONESIGNAL_API_KEY, ONESIGNAL_APP_ID)
> --
> 
>     local decoded = nk.json_decode(payload)
> 
>     local user_ids = decoded.user_ids
>     local player_ids = {}
>     local users = nk.users_get_id(user_ids)
> 
>     for _, u in ipairs(users)
>         do
>           table.insert(player_ids, u.metadata.os_player_id)
>         end
> 
>     local contents = decoded.contents
>     local headings = decoded.headings
> 
> 
>     local included_segments = nil
>     local filters = nil
>     local params = {}
>     onesignal:create_notification(contents, headings, included_segments, filters, player_ids, params)
> 
>     return payload
> end
> 
> nk.register_rpc(send_push, "send_push")