Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when sending a GCM notification #629

Closed
jdtaylor91 opened this issue Mar 1, 2016 · 5 comments
Closed

NullReferenceException when sending a GCM notification #629

jdtaylor91 opened this issue Mar 1, 2016 · 5 comments

Comments

@jdtaylor91
Copy link
Contributor

What version of PushSharp are you using?

PushSharp v3.0.1.0

Describe your issue:

A [System.NullReferenceException] is thrown when sending a GCM notification.

I am in the process of upgrading from PushSharp v2 to PushSharp v3 and I have been having quite a few issues with the GCM notifications. The issues stemmed from trying to use the 'To' property of the GcmNotification.cs class, populating this results in a HTTP 400 response from GCM, due to the following reason - "Must use either "registration_ids" field or "to", not both".

PushSharp v3 does not currently validate this and if a GcmNotification is created using the constructor AND the 'To' property is populated it will result in a HTTP 400 response as the following JSON is submitted:
{ "registration_ids":[], "to": "regtokenstring" }

To work around this i manually set RegistrationIds to NULL, this then resulted in the below NullReferenceException as the GcmNotification is expecting RegistrationIds to be populated.

Line 17 of GcmNotification.cs should be updated to first check that RegistrationIds is null.

Also if possible it would be nice to have any error messages from GCM passed through the PushSharp library, currently any HTTP 400 response results in a GcmConnectionException being thrown, which ends up with any meaningful error message from GCM being lost.

What are the steps required to reproduce this issue?

Send a GcmNotification with the 'To' field populated and the 'RegistrationIds' field as null.

Please provide any Exception Stack Traces

[System.NullReferenceException] Object reference not set to an instance of an object. at PushSharp.Google.GcmNotification.ForSingleResult(GcmResponse response, Int32 resultIndex) at PushSharp.Google.GcmServiceConnection.<processResponseOk>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at PushSharp.Google.GcmServiceConnection.<Send>d__6.MoveNext()

@joeljeske
Copy link

+1
I am also experiencing this issue. It is preventing me from sending any GCM notifications.

@jdtaylor91
Copy link
Contributor Author

joeljeske - there is a work around for this. Instead of populating the 'To' field, just populate 'RegistrationIds', this avoids the HTTP 400 response and also avoids the NullReferenceException.

So something like:
var notification = new GcmNotification() { RegistrationIds = new List<string> { "reg_id" }, ... };

instead of:
var notification = new GcmNotification() { To = "reg_id", ... }

Redth added a commit that referenced this issue Mar 5, 2016
@Redth
Copy link
Owner

Redth commented Mar 5, 2016

I committed some code that should help with this. Basically, it will look and see if you specified To, and if there are no registraton id's specified, and if so, when serializing, it will exclude the registrationIds = [] from the json.

Could someone please try this out from master and confirm it fixes their issue?

jdtaylor91 added a commit to jdtaylor91/PushSharp that referenced this issue Mar 7, 2016
Fix for NullReferenceException issue -
Redth#629

Additional fix for retrieving oldRegistrationId from the 'To' field if
'RegistrationIds' is empty or null
@jdtaylor91
Copy link
Contributor Author

There was still an issue resulting in the NullReferenceException, it should be fixed in my pull request above. Your commit has removed the HTTP 400 response from GCM though - thanks.

@Redth
Copy link
Owner

Redth commented Mar 9, 2016

Closing this as the previous request was merged. Thanks for everyone's help and input!

@Redth Redth closed this as completed Mar 9, 2016
Redth added a commit that referenced this issue May 10, 2016
Redth pushed a commit that referenced this issue May 10, 2016
Fix for NullReferenceException issue -
#629

Additional fix for retrieving oldRegistrationId from the 'To' field if
'RegistrationIds' is empty or null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants