【问题标题】:How can I send an Apple Push Notification with multiple lines i.e. with a '\n' character?如何发送多行的 Apple 推送通知,即带有 '\n' 字符?
【发布时间】:2011-10-09 21:56:49
【问题描述】:

我想知道如何发送多行的苹果推送通知消息。使用 '\n' 似乎不起作用。

类似:

第一行

第二行

现在它似乎完全忽略了这条消息。

【问题讨论】:

  • iOS 似乎忽略了错误编码的消息,而 Android 显示它们的代码不完整。

标签: ios push-notification


【解决方案1】:

你不能通过转义发送多行推送,它不起作用!

只是尝试使用 Parse 发送推送:

有效载荷没有转义:

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

结果:

带有转义的有效载荷

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

结果:

【讨论】:

  • 嗨,Johnny,我正在尝试使用 APNSwrapper 发送推送通知。但是设备上没有任何反应。从进口APNSWrapper *#mylocal_DeviceToken = 'e0cf72eaaf87ef73be7a9687a5eb16caaae8da63' mylocal_PushMagic = '9B6A010D-52C0-477C-9C67-59CCE8AB77A2' 包装= APNSNotificationWrapper( 'PushCert.pem',假)消息= APNSNotification()message.tokenBase64( '5755bfa7297b765cd03fdc6f47a137ef1e9126ef')#message .token(mylocal_DeviceToken) message.badge(5) message.appendProperty(APNSProperty('mdm', mylocal_PushMagic)) wrapper.append(message) wrapper.notify() 请帮忙
【解决方案2】:

添加一个可本地化的字符串文件并在那里添加您的字符串。例如,你可以有类似的东西:

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

现在在您的通知负载中,使用 loc-key 和 loc-args 属性,例如:

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

现在您的通知中应该有一个换行符。

【讨论】:

    【解决方案3】:

    对字符串使用双引号,例如:

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

    【讨论】:

    • 在 iOS 12 中为我工作。我猜 \r 是回车。
    【解决方案4】:

    我在 PHP 中使用 chr(10) 来发送换行符作为推送消息的一部分,例如。

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

    【讨论】:

    • 这适用于我在 iOS 上使用 One Signal 进行最终交付。
    • 也适用于 firebase,即使使用 $text = str_replace('\n', chr(10), $text);
    【解决方案5】:

    我想通了:esacpae \n。呵呵。

    所以使用:

    First line \\n
    Second Line
    

    而不是

    First line \n
    Second Line
    

    【讨论】:

    • 这不应该被否决。当最终的 JSON 字符串包含 \\n 时,它确实有效。请记住,您的编程语言可能已经为您执行了该转换,因此如果您还有再次转义反斜杠的逻辑,那将导致问题。
    【解决方案6】:

    这里有你想知道的解决方案!

    第一行 \r\n 第二行

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

        你可以试试:

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

        【讨论】:

          【解决方案9】:

          也许您正在寻找的是subtitle 属性。见Multi line title in push notification for iOS

          【讨论】:

            【解决方案10】:

            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”

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-29
              • 1970-01-01
              • 2017-11-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多