Search code examples
c#cordovapush-notificationconsole-applicationpushsharp

PushSharp “get json on NotificationSent ” issue


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);

Solution

  • 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 ();
      }
    }