How can I send an Apple Push Notification with multiple lines i.e. with a '\n' character?
Asked Answered
C

9

10

I would like to know how to send an apple push notification message with multiple lines. Using '\n' doesn't seem to work.

Something like:

First line

Second Line

Right now it seems to just ignore the message altogether.

Coming answered 9/10, 2011 at 21:56 Comment(1)
iOS seems to ignore wrongly encoded messages, while Android shows them with broken code.Solarium
C
8

You cannot send multi line push with a escape, it will not work!

simply tried to send push with Parse:

Payload without an escape:

{ "alert": "Send me\na push without escape", "sound": "default" }

Result: enter image description here

Payload with an escape

{ "alert": "Send me\\na push with escape", "sound": "default" }

Result:

enter image description here

Chretien answered 2/6, 2015 at 7:14 Comment(1)
Hi Johnny I am trying to send push notification using APNSwrapper. BUt nothing happens on device. from APNSWrapper import * #mylocal_DeviceToken='e0cf72eaaf87ef73be7a9687a5eb16caaae8da63' mylocal_PushMagic='9B6A010D-52C0-477C-9C67-59CCE8AB77A2' wrapper = APNSNotificationWrapper('PushCert.pem', False) message = APNSNotification() message.tokenBase64('5755bfa7297b765cd03fdc6f47a137ef1e9126ef') #message.token(mylocal_DeviceToken) message.badge(5) message.appendProperty(APNSProperty('mdm', mylocal_PushMagic)) wrapper.append(message) wrapper.notify() please helpGandzha
B
7

I have used chr(10) in PHP to send a newline as part of a push message, eg.

$message = "Hey {user_firstname}! " . chr(10) . "you have a new message!"

Bendicty answered 27/7, 2017 at 15:47 Comment(2)
This worked for me on iOS using One Signal for end delivery.Gondolier
Works with firebase too, even when using $text = str_replace('\n', chr(10), $text);Solarium
S
5

Add a localizable strings file and add your string there. For example, you could have something like:

"Push_String" = "My push string with a line break\n and argument: %@";

Now in your notification payload, use the loc-key and loc-args properties, for example:

"loc-key":"Push_String","loc-args":["My argument!"]

Now you should have a line break in your notification.

Supersonics answered 18/2, 2015 at 20:51 Comment(0)
D
4

use double quote for string like:

string = "first line  \r\n Second line ";
Disoblige answered 28/1, 2016 at 10:30 Comment(1)
Worked for me in iOS 12. I guess \r is a carriage return.Highflier
C
2

I figured it out: esacpae the \n. Duh.

so use:

First line \\n
Second Line

instead of

First line \n
Second Line
Coming answered 9/10, 2011 at 21:58 Comment(1)
This shouldn't be down-voted. It does in fact work when the final JSON string contains \\n. Keep in mind that your programming language may already be performing that conversion for you, so if you also have logic that is escaping the backslash again, that will cause problems.Dulcia
U
1

Apple push will reject a string for a variety of reasons. I tested a variety of scenarios for push delivery, and this was my working fix (in python):

#  Apple rejects push payloads > 256 bytes (truncate msg to < 120 bytes to be safe)
if len(push_str) > 120:
    push_str = push_str[0:120-3] + '...'

# Apple push rejects all quotes, remove them
import re
push_str = re.sub("[\"']", '', push_str)

# Apple push needs to newlines escaped
import MySQLdb
push_str = MySQLdb.escape_string(push_str)

# send it
import APNSWrapper
wrapper = APNSWrapper.APNSNotificationWrapper(certificate=...)
message = APNSWrapper.APNSNotification()
message.token(...)
message.badge(1)
message.alert(push_str)
message.sound("default")
wrapper.append(message)
wrapper.notify()
Unhouse answered 12/6, 2014 at 14:19 Comment(0)
G
0

you can try:

alert:"this is 1st line \\n"
Gerik answered 13/10, 2017 at 0:24 Comment(0)
F
0

Perhaps what you're looking for is the subtitle attribute. See Multi line title in push notification for iOS

Forspent answered 9/8, 2018 at 22:56 Comment(0)
J
0

If your payload have "\\n" do this:

parse first your payload like this

title = First line \nSecond Line //either \n or \\n
title = title.replacingOccurrences(of: "\\\\n", with: "\n", options: .regularExpression)

the best solution is to make your payload "\n" or "\r\n"

Jezabel answered 22/4, 2020 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.