开发者

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

I would like to know how to send an apple push notification message with multiple lines. Usi开发者_运维知识库ng '\n' doesn't seem to work.

Something like:

First line

Second Line

Right now it seems to just ignore the message altogether.


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:

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

Payload with an escape

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

Result:

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


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.


use double quote for string like:

string = "first line  \r\n Second line ";


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!"


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

so use:

First line \\n
Second Line

instead of

First line \n
Second Line


Here's a solution you want to know!

First line \r\n Second line


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()


you can try:

alert:"this is 1st line \\n"


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


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜