0

I want to read the iOS push "alert" message

parse.com has this example on their website

ParsePush.ParsePushNotificationReceived += (sender, args) => {
  var payload = args.Payload;
  object objectId;
  if (payload.TryGetValue("objectId", out objectId)) {
    DisplayRichMessageWithObjectId(objectId as string);
  }
};

But how do I read the alert message from the payload?

Solution

string message = "";

try
{
    var payload = args.Payload;
        object aps;
        if (payload.TryGetValue("aps", out aps))
        {
            string payloadStr = "";
                try
                {
                    payloadStr = aps.ToString();
                }
                catch (Exception e)
                {
                }
                try
                {
                    var match = Regex.Match(payloadStr, @"alert = (.*);\n");
                        if (match.Success)
                        {
                            string alertText = match.Groups[1].Value;
                                message = alertText;
                        }
                }
                catch (Exception)
                {
                }
    }
}

1 Answer 1

1

Try this:

ParsePush.ParsePushNotificationReceived += (sender, args) => {
  var payload = args.Payload;
  object aps;
  if (payload.TryGetValue("aps", out aps)) {
    string payloadStr = aps as string;
  }
};

Also, there should be a args.PayloadString which should give some clues about the structure of the payload.

Sign up to request clarification or add additional context in comments.

6 Comments

args.PayloadString returns null but I can see that args.Payload itself contains aps which contains what I want. Sadly I can't try this right now but on monday.
Hi again. This is what it returns i.imgur.com/zNeS3qB.png Do you know any good way of getting the "alert" part out or should I use substrings?
You need to parse it into a JSON object then you can grab the "alert" value
But neither the object or the value is proper JSON
Sorry you're right, looked like JSON at a glance. Maybe use a regex to extract it? Something like: alert = "(.*)"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.