【发布时间】:2011-10-09 21:56:49
【问题描述】:
我想知道如何发送多行的苹果推送通知消息。使用 '\n' 似乎不起作用。
类似:
第一行
第二行
现在它似乎完全忽略了这条消息。
【问题讨论】:
-
iOS 似乎忽略了错误编码的消息,而 Android 显示它们的代码不完整。
标签: ios push-notification
我想知道如何发送多行的苹果推送通知消息。使用 '\n' 似乎不起作用。
类似:
第一行
第二行
现在它似乎完全忽略了这条消息。
【问题讨论】:
标签: ios push-notification
你不能通过转义发送多行推送,它不起作用!
只是尝试使用 Parse 发送推送:
有效载荷没有转义:
{ "alert": "Send me\na push without escape", "sound": "default" }
结果:
带有转义的有效载荷
{ "alert": "Send me\\na push with escape", "sound": "default" }
结果:
【讨论】:
添加一个可本地化的字符串文件并在那里添加您的字符串。例如,你可以有类似的东西:
"Push_String" = "My push string with a line break\n and argument: %@";
现在在您的通知负载中,使用 loc-key 和 loc-args 属性,例如:
"loc-key":"Push_String","loc-args":["My argument!"]
现在您的通知中应该有一个换行符。
【讨论】:
对字符串使用双引号,例如:
string = "first line \r\n Second line ";
【讨论】:
我在 PHP 中使用 chr(10) 来发送换行符作为推送消息的一部分,例如。
$message = "Hey {user_firstname}! " . chr(10) . "you have a new message!"
【讨论】:
$text = str_replace('\n', chr(10), $text);
我想通了:esacpae \n。呵呵。
所以使用:
First line \\n
Second Line
而不是
First line \n
Second Line
【讨论】:
这里有你想知道的解决方案!
第一行 \r\n 第二行
【讨论】:
Apple push 会出于多种原因拒绝一个字符串。我测试了各种推送交付场景,这是我的工作修复(在 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()
【讨论】:
你可以试试:
alert:"this is 1st line \\n"
【讨论】:
也许您正在寻找的是subtitle 属性。见Multi line title in push notification for iOS
【讨论】:
If your payload have "\\n" do this:
首先像这样解析你的有效载荷
title = First line \nSecond Line //either \n or \\n
title = title.replacingOccurrences(of: "\\\\n", with: "\n", options: .regularExpression)
最好的解决方案是让你的有效载荷“\n”或“\r\n”
【讨论】: