Amazon Simple Notification Service with iOS Custom Payload Not So Easy

Sending a simple text notification is simple and well-documented. But today, I pulled my hair for sending a custom notification for iOS that has a warning, and some fields like userId.

I started from this help page and implemented something similar to the last sample, then I found this answer , which seems to nullify the last sample on the help page, since the url property must be outside the aps object. I tried many combinations, but each of them is sent as text to the application (the whole message with the property "default" and "APNS") ...

If I explicitly set the MessageStructure to json, I get the error: "Invalid parameter: Message Structure - the body of the JSON message could not be parsed," but I'm sure my JSON is good when sending SNS in a line in the Message property looks like this:

{ "default":"You received a new message from X.", 
 "APNS_SANDBOX":"{ \"aps\": {\"alert\":\"You received a new message from X.\"}, 
                \"event\":\"Message\", 
                \"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"
            }", 
 "APNS":"{ \"aps\": {\"alert\":\"You received a new message from X.\"}, 
                \"event\":\"Message\", 
                \"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"
            }" 
}

Does anyone have a good example of sending custom payload notifications via SNS in C #? Because Amazon is sure not ... Thank you!

+4
source share
2 answers

, , , , . ... , :

{"default":"You received a new message from X.","APNS_SANDBOX":"{\"aps\":{\"alert\":\"You received a new message from X.\"},\"event\":\"Message\",\"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"}","APNS":"{\"aps\":{\"alert\":\"You received a new message from X.\"},\"event\":\"Message\",\"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"}"}

, ( APNS ), Event ObjectID:

[DataContract]
public class AmazonSNSMessage
{
    [DataMember(Name = "default")]
    public string Default { get; set; }

    [DataMember(Name = "APNS_SANDBOX")]
    public string APNSSandbox { get; set; }

    [DataMember(Name = "APNS")]
    public string APNSLive { get; set; }

    public AmazonSNSMessage(string notificationText, NotificationEvent notificationEvent, string objectID)
    {
        Default = notificationText;
        var apnsSerialized = JsonConvert.SerializeObject(new APNS
        {
            APS = new APS { Alert = notificationText },
            Event = Enum.GetName(typeof(NotificationEvent), notificationEvent),
            ObjectID = objectID
        });
        APNSLive = APNSSandbox = apnsSerialized;
    }

    public string SerializeToJSON()
    {
        return JsonConvert.SerializeObject(this);
    }
}

[DataContract]
public class APNS 
{
    [DataMember(Name = "aps")]
    public APS APS { get; set; }

    [DataMember(Name = "event")]
    public string Event { get; set; }

    [DataMember(Name = "objectID")]
    public string ObjectID { get; set; }
}

[DataContract]
public class APS
{
    [DataMember(Name = "alert")]
    public string Alert { get; set; }
}

, Amazon SNS:

new AmazonSNSMessage(...).SerializeToJSON();
+8

, , , JSON, SNS (, " " "APNS" ), , . , Message ( ):

{"APNS":"{\"aps\" ... 

, "APNS" , ( , ), . :

JObject payload = ... // your apns, etc payload JObject

var snsMsgJson = new JObject(
    new JProperty("default", "message 1 2 3"), // "default" is optional if APNS provided
    new JProperty("APNS", payload.ToString(Formatting.None))
);

string str = snsMsgJson.ToString(Formatting.None);

, , ! , , " " . , : " ... , ", . , , ( SNS) JSON , .

: ? API, : . , .

+1

Source: https://habr.com/ru/post/1610339/


All Articles