具有图像无法使用推送通知的可跨越字符串

时间:2016-04-21 12:30:46

标签: android push-notification

我正在尝试在推送通知中使用带有图像的spannable字符串。它不起作用。其实我想在像wattsapp这样的推送通知上显示表情符号。如果我使用简单的spannable字符串它是有效的。带有图像的相同spannable字符串与textview一起正常工作。

为此,我也尝试过自定义推送通知,但没有取得成功。

我的代码如下: -

MainActivity.class

   package com.example.tt.notificationtest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;

public class MainActivity extends Activity {
    Button btnNotification;
    TextView txtMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtMsg = (TextView) findViewById(R.id.txtMsg);
        btnNotification = (Button) findViewById(R.id.btnNotification);
        btnNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Notify("Testing", "msg");
                Notify2();
            }
        });
    }

    private void Notify2(){
        int icon = R.drawable.asdf;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.asdf);

        SpannableString ss = new SpannableString("abcd");
        Drawable d = getResources().getDrawable(R.drawable.asdf);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, ss);
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);
    }

    private void Notify(String notificationTitle, String notificationMessage){

        //This is working

//        SpannableString ss = new SpannableString("Large\n\n" );   // index 103 - 112
//        ss.setSpan(new BackgroundColorSpan(Color.CYAN), 0, 5, 0);



        //this is not working
        SpannableString ss = new SpannableString("abcd");
        Drawable d = getResources().getDrawable(R.drawable.asdf);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        txtMsg.setText(ss);

        Intent i = new Intent(MainActivity.this, MainActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this, 0,
                i,
                0);

        Notification notification = new NotificationCompat.Builder(MainActivity.this)
                .setContentTitle(notificationTitle)
                .setContentText(ss)
                .setSmallIcon(R.drawable.asdf)
//                .setLargeIcon(bitmap)
//                .setDefaults(Notification.DEFAULT_ALL)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(ss))
                .setAutoCancel(true)
                .setContentIntent(pi)
                .build();

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(2, notification);
    }

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tt.notificationtest.MainActivity">

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:id="@+id/btnNotification"/>
</LinearLayout>

custom_notification.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toRightOf="@id/image" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:3)

这是我开展工作的唯一方法:

跨越mssg; mssg = Html.fromHtml(URLDecoder.decode(“%F0%9F%98%82”));

它可能会有所不同,具体取决于您如何收到文本,在我的情况下,我使用unicode将表情符号发送到GCM服务器,我不必担心URLDecoder,但如果我收到从一个emojicon PHP库,而不是来自Android设备唯一正确显示它的方法是使用URLDecoder。

因此,如果您正在使用GCM,它应该只使用

跨越mssg; mssg = Html.fromHtml(intent.getStringExtra(“message”));

希望它有所帮助!

答案 1 :(得分:3)

Mixpanel具有在推送通知中发送表情符号的工作解决方案。您可以找到说明here

答案 2 :(得分:1)

您在这里使用弃用的代码

Notification notification = new Notification(icon, "Custom Notification", when);

enter image description here

而是使用像NotificationBuilder.sentContent(remoteView)

这样的RemoteView

看看这个Custom Notification示例