0

你好朋友我想用自定义布局显示通知,因为还有大的图像视图也可用,下面的加载表单 url 是我的代码

<RelativeLayout
    android:id="@+id/rel_top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:src="@drawable/app_logo"/>

    <TextView
        android:id="@+id/noti_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textStyle="bold" 
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/img"
        android:layout_alignTop="@+id/img"
        android:textSize="@dimen/text_16"
        />
    <TextView
        android:id="@+id/noti_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textStyle="normal"
        android:layout_marginLeft="5dp"
        android:layout_below="@+id/noti_first"
        android:layout_toRightOf="@+id/img"
        android:textSize="@dimen/text_15"
        />
    <ImageView
        android:id="@+id/img_preview"
        android:layout_width="match_parent"
        android:layout_below="@+id/img"
        android:maxHeight="100dp"
        android:layout_marginTop="@dimen/margin_5dp"
        android:layout_height="100dp"
        android:src="@drawable/app_logo"/>
</RelativeLayout>

GCMReceiver 类

public class GCMReceiver extends BroadcastReceiver {
private String TAG = GCMReceiver.class.getName();
private NotificationManager mNotificationManager;

@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
    try {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .build();
        StrictMode.setThreadPolicy(policy);

        System.out.println("Receive");
        String title = intent.getExtras().getString("title");
        String message = intent.getExtras().getString("message");
        String description = intent.getExtras().getString("description");
        String product_id = intent.getExtras().getString("product_id");
        String image = intent.getExtras().getString("image");
        String url = intent.getExtras().getString("url");

        Log.d("Title", title);
        Log.d("description", description);
        Log.d("Image", image);
        Log.d("url", url);
        Tags.mStringViewAllCategory = url;
        if (title != null && title.trim().length() > 0) {
            Random random = new Random();

            mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notifyIntent = new Intent(context, ActivityCategoryViewAll.class);
            notifyIntent.putExtra("title", title);
            notifyIntent.putExtra("Description", description);
            notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            PendingIntent contentIntent = PendingIntent.getActivity(context, random.nextInt(), notifyIntent, 0);
            RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
            expandedView.setTextViewText(R.id.noti_first, title);
            expandedView.setTextViewText(R.id.noti_second, description);
            Bitmap bitmap = getBitmapFromURL("https://s3.amazonaws.com/atbdev/cache/product/soie-western-wear-off-white-color-top-fl-uphQF570b7bbbd6033.jpg/3c7d9ab9f983e1598d7c00c524e5d5c0");

            expandedView.setImageViewBitmap(R.id.img_preview, bitmap);

            Notification notification = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.app_logo)
                    .setAutoCancel(true)
                    .setContentIntent(contentIntent)
                    .setContentTitle(title).setContentText(message).build();
            notification.defaults |= Notification.DEFAULT_SOUND;
            mNotificationManager.notify(random.nextInt(), notification);
        }
    } catch (Exception e) {
        Log.e(TAG, "onReceive >> " + e.toString());
    }
}

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
     }
    }

 }

当我运行上面的代码通知完美但不显示 URL 图像时,我该如何解决这些问题?你的所有建议都是可观的。

编辑

Notification notification = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.app_logo)
                    .setAutoCancel(true)
                    .setContentIntent(contentIntent)
                    .setContent(expandedView)
                    .setContentTitle(title).setContentText(message).build();
            notification.defaults |= Notification.DEFAULT_SOUND;
            mNotificationManager.notify(random.nextInt(), notification);

截图

在此处输入图像描述

4

1 回答 1

0

在您的代码中,您将标题和内容设置为默认通知,但对于自定义通知,您需要使用您的远程视图设置内容,即扩展视图

下面的代码可能会帮助你

   Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.app_logo)
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setContent(expandedView).build();
    notification.bigContentView = expandedView;
于 2016-04-30T07:51:30.120 回答