我怎样才能发送一个苹果推送通知多行,即与'\ n'字符?
我想知道如何发送多行的苹果推送通知消息。 使用'\ n'似乎不起作用。
就像是:
第一行
第二行
现在,似乎完全忽略了这个信息。
这是你想知道的解决scheme!
第一行\ r \ n第二行
添加一个可本地化的string文件,并添加您的string。 例如,你可能有这样的东西:
"Push_String" = "My push string with a line break\n and argument: %@";
现在在通知负载中,使用loc-key和loc-args属性,例如:
"loc-key":"Push_String","loc-args":["My argument!"]
现在,您的通知应该有一个换行符。
你不能发送多行推送逃生,这将无法正常工作!
只是尝试发送推动与parsing:
无需转义的有效载荷:
{ "alert": "Send me\na push without escape", "sound": "default" }
结果:
用转义负载
{ "alert": "Send me\\na push with escape", "sound": "default" }
结果:
由于各种原因,Apple推送会拒绝一个string。 我testing了各种场景推送,这是我的工作修复(在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()
使用双引号string如:
string = "first line \r\n Second line ";
我已经在PHP中使用chr(10)作为推送消息的一部分发送换行符,例如。
$message = "Hey {user_firstname}! " . chr(10) . "you have a new message!"
我明白了:esacpae the \ n。 咄。
所以使用:
First line \\n Second Line
代替
First line \n Second Line
你可以试试:
alert:"this is 1st line \\n"