问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/36905361/structuring-gcm-messages-in-pushsharp-4-0