-1

I used PushSharp DDL, I am trying to save the status of the notification send in my database. on NotficationSent Event, I will update my database with status=true where NotificationID=XXXX NotficationSent event includes my JSON which I pushed in parameter (notification)

I try to get my JSON in SentNotification Event to know My NotificationID I wrote this code but it did not work.

static void NotificationSent(object sender, INotification notification)
    {
var push = (PushSharp.Android.GcmNotification)notification;
    json =Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic(push.JsonData);
    var NotificationID=json.NotificationID
    }

the code is not completed run it stopped at this line with no error and the function is exit, I can not get NotificationID in my variable

json =Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic(push.JsonData);
rol
  • 57
  • 2
  • 7

1 Answers1

0

INotification is just an interface and the JsonData property is not part of that interface. The parameter instance passed in may not in fact be a GcmNotification type, so you should check to ensure it is and then case to it:

static void NotificationSent(object sender, INotification notification)
{
  var gcmNotification = notification as GcmNotification;
  if (gcmNotification != null) {
    var json = JObject.Parse (gcmNotification.JsonData);
    var notificationId = json["NotificationID"].ToString ();
  }
}
Redth
  • 5,464
  • 6
  • 34
  • 54
  • thank you, this as my code, but when I run and debug this code, the code not completed it stopped at below line and exit from the function var json = Newtonsoft.Json.JsonConvert.DeserializeObject(gcmNotification.JsonData); after above line the rest of the code not run to get NotificationID this line did not run because the function is exit var notificationId = json.NotificationID; – rol Jan 25 '16 at 15:54
  • I updated my sample to not use deserialization... This should just grab the `NotificationID` value from the json object. Keep in mind `JsonData` has whatever *you* put in it, so you are responsible for adding the `NotificationID` key/value pair if you want it available in that event. – Redth Jan 25 '16 at 16:35