开发者

c2dm - Sending data with multiple lines

I have got a problem with m开发者_运维知识库ulti line data (data that contains newline \n). These data is beeing encoded for the url call:

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

I have no problem sending c2dm messages. But when I try to get my string back from the intent, der is no newline sign.

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

I guess there has been some decoding of the urlstring inside the C2DM Framework and that removed all special charaters?

It would be nice, if somebody can help me or can confirm my guess.


I found another way by modify C2DMReceiver.java as documented custom view for notification bar.

c2dm - Sending data with multiple lines

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);
    }

and create layout custom_notification_layout.xml like this

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

Hope this helps.

Reference: Android SDK Doc


I'm going to assume you're not double URL-encoding the form parameters.

On the Android side, is there any character(s) at all between Line1 and Line2?

I think the problem might be that you are including the URL encoded form of a linefeed %0a but that's not a newline from a Java String's point of view. Have you tried sending \n instead?

Also, on the device side, you may need to unescape the incoming data to turn the escape sequence back into an actual Java newline. For example, using code similar to Apache Commons' StringEscapeUtils.unescapeJava() (not part of Android AFAIK, but the source is available)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜