3

I'm somewhat confused in how I'm supposed to structure the message body for a GCM push notification using PushSharp. The docs as well as the test files in the GitHub repo show what looks to be the message structure as this:

            broker.QueueNotification (new GcmNotification {
                RegistrationIds = new List<string> { 
                    regId
                },
                Data = JObject.Parse ("{ \"somekey\" : \"somevalue\" }")
            });

I've been using Postman for testing in which I send a message in the following JSON format to https://gcm-http.googleapis.com/gcm/send.

{
    "to": "000-0000-mytoken-foo",
    "notification" : {
        "title" : "Push Test Notification",
        "body" : "GCM Test Push",
        "icon" : "icon",
        "color" : "#FF4081"
    }

}

Is the 'to' and the 'notification' sections of the message already taken care of by the framework? Based off the examples I've seen it seems as though I only need to enter the key:value pairs of the notification object but I can't seem to find where that is stated or an example of an actual message in the docs. I'm using the newest version (4.x) of PushSharp.

Stavros_S
  • 2,145
  • 7
  • 31
  • 75
  • Did you find out how it is set up? I am getting success messages but no notification arrives on Android – Lukas Jun 15 '16 at 15:00
  • 1
    @pechar Yes, you probably want to send a notification rather than a data message. Change the `Data = JObject.Parse` to `Notification = JObject.Parse`. I'll post a more flesh out answer with examples a bit later. – Stavros_S Jun 15 '16 at 15:12
  • Thanks for your reply. I actually managed to get it to run in the evening. I used the `Data` property anyway since the Android App handles the notification using the data passed there. However I guess it all depends on how your app takes care of the notification. `to` can also be assigned in the separate properties of the `GcmNotification` the same way as `notification` – Lukas Jun 16 '16 at 09:22
  • @pechar Do you by any chance know how to request a token refresh from Push Sharp? – Stavros_S Jun 16 '16 at 13:36
  • I am not sure I understand what you mean but usually the token refresh needs to happen on the device. The new token is then sent to your server. There is an Exception you can capture `OnNotificationFailed` where you check the exception parameter and if it is a `DeviceSubscriptionExpiredException` then you can get the `OldSubscriptionId` and `NewSubscriptionId`. You can then replace your old id in the database with the new one. The subscription ids here are the tokens. – Lukas Jun 16 '16 at 14:04

1 Answers1

2

Hi there you can go like this

var config = new GcmConfiguration("senderKey", "apiKey", null);
config.GcmUrl = "https://fcm.googleapis.com/fcm/send"; //!!!!!gmc changed to fcm;)

var gcmBroker = new GcmServiceBroker(config);
gcmBroker.Start();

_gcmBroker.QueueNotification(new GcmNotification
                {
                    RegistrationIds = new List<string> { "YourDeviceToken" },
                    Notification = JObject.Parse(
                        "{" +
                            "\"title\" : \"" + yourMessageTitle + "\"," +
                            "\"body\" : \"" + yourMessageBody + "\"," +
                            "\"sound\" : \"mySound.caf\"" +
                        "}") ,
                    Data = JObject.Parse(
                        "{" +                            
                            "\"CustomDataKey1\" : \"" + yourCustomDataValue1 + "\"," +
                            "\"CustomDataKey2\" : \"" + yourCustomDataValue2 + "\"" +
                        "}")
                });

I did not test if the custom data values are arriving but the notification does:) The items starts with "your" are your dynamic arguments like parameters that you pass to that method etc. The question asked long time ago but i started to use pushsharp just now :) Hope this helps to PushSharp users.

Umut Bebek
  • 364
  • 4
  • 9
  • What is the error? Did you registered to the events of the _gcmBroker? for example: _gcmBroker.OnNotificationSucceeded += notification => { //Console.WriteLine("Android Notification Sent!"); }; so what do you receive from this events? @AhmadArslan – Umut Bebek Mar 15 '17 at 08:57