c2dm - 使用多行发送数据

时间:2011-06-26 15:52:42

标签: android android-c2dm

我遇到了多行数据的问题(包含换行符的数据\ n)。这些数据正在为网址调用编码:

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

发送c2dm消息没问题。但是当我试图从意图中恢复我的字符串时,der不是换行符号。

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

我想在C2DM框架内对urlstring进行了一些解码并删除了所有特殊的字符?

如果有人可以帮助我或确认我的猜测,那就太好了。

2 个答案:

答案 0 :(得分: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 Doc

答案 1 :(得分:1)

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

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

我认为问题可能是你包含了换行符%0a的URL编码形式,但这不是Java String的新视角。您是否尝试过发送\n

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

相关问题