C2DM - 发送多行数据

4

我遇到了一个多行数据的问题(即包含换行符\n的数据)。这些数据正在进行URL调用的编码:

collapse_key=<my-collapse_key>&registration_id=<my-registrationd>
&data.text=Line1%0ALine2&data.group=This+is+a+test

我没有问题发送c2dm消息。但是当我尝试从意图中获取我的字符串时,没有换行符。

    text = (String) intent.getStringExtra("text");

我猜测C2DM框架内部对urlstring进行了解码,这可能会删除所有特殊字符。如果有人能帮助我或确认我的猜测,那就太好了。

为什么在消息中需要换行? - Glendon Trullinger
2个回答

5
我发现了另一种方法,通过修改 C2DMReceiver.java 作为通知栏的自定义视图来实现。 enter image description here
protected void sendnotification (String title, String message) {
       String ns = Context.NOTIFICATION_SERVICE;

       NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

       int icon = R.drawable.icon100;

       RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
       contentView.setImageViewResource(R.id.image, R.drawable.icon100);
       contentView.setTextViewText(R.id.text, message);

       CharSequence tickerText = message;
       long when = System.currentTimeMillis();

       Notification notification = new Notification(icon, tickerText, when);
       notification.contentView = contentView;

       Intent notificationIntent = new Intent(this, Startup.class);

       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

       notification.flags = Notification.FLAG_AUTO_CANCEL;
       notification.contentIntent = contentIntent;

       Random randInt = new Random();
       int id = randInt.nextInt(100) - 1;
       mNotificationManager.notify(id, notification);
    }

并创建名为custom_notification_layout.xml的布局文件,内容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="3dp"
              >
    <ImageView android:id="@+id/image"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_marginRight="10dp"
              />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#000"
              />
</LinearLayout>

希望这能帮到你。
参考资料:Android SDK文档

1

我假设您没有对表单参数进行双重URL编码。

在Android端,Line1Line2之间是否有任何字符?

我认为问题可能是您包含了换行符的URL编码形式%0a,但从Java字符串的角度来看,这不是一个换行符。您尝试过发送\n吗?

此外,在设备端,您可能需要取消转义传入的数据,将转义序列转换回实际的Java换行符。例如,使用类似于Apache Commons' StringEscapeUtils.unescapeJava()的代码(Android AFAIK不包括在内,但源代码可用)。


我已经尝试了各种可能的方式来取消转义,包括UrlDecode...现在我通过在服务器端和Androd设备上再次替换新行来解决了这个问题。但还是谢谢。 - Mark
如果您发布您正在使用的代码,也许我们可以帮助您解决这个问题。我已经成功地使用上述技术接收包含换行符的字符串。 - gnuf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接