2016-04-30 9 views
0

Здравствуйте, друг, я хочу показать уведомление с пользовательским макетом и в этом есть большая ImageView также какая форма загрузки URL ниже мой кодпользовательских уведомлений с большим ImageView не отображает что нагрузки формы URL в RemoteView андроид

<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 изображения не отображаются какие-либо идеи, как я могу решить эти проблемы? все ваши предложения заметны.

EDIT

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

Скриншоты

enter image description here

+0

Вы не устанавливая 'RemoteViews' на' Notification' в любом месте. –

+0

Майк М. Какой способ сообщить мне, когда я ошибаюсь? –

ответ

0

В своем коде вы устанавливаете титул & содержание как Notificatio по умолчанию, но для пользовательского уведомления вам необходимо setContent с вашим remoteView ie extendedView

Ниже кода может помочь вам

Notification notification = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.app_logo) 
       .setAutoCancel(true) 
       .setContentIntent(contentIntent) 
       .setContent(expandedView).build(); 
    notification.bigContentView = expandedView; 
+0

Ankii Rawat: см. Мою часть редактирования, которую я установил как epr ваше предложение, но я до сих пор не получаю url image –

+0

Вам не нужно использовать '.setContentTitle (title) setContentText (message)' –

+0

Смотрите мою часть редактирования, пока я не получаю url image [http://pastebin.com/kT14FuaC](http://pastebin.com/kT14FuaC) –