Android – 在Facebook等通知栏中使用外部个人资料图片

我知道你可以发送消息,标题,图像URL等推送通知参数的信息。Facebook如何显示你的消息在通知区域的个人资料图片? 我想在通知区域中使用外部图像,因此,当您将其拉下时,会看到包含消息的configuration文件图像。 现在,我只显示可绘制文件夹中的默认图标。 我想这可能是一个常见的问题,但找不到任何东西。 你能帮忙的话,我会很高兴。

Solutions Collecting From Web of "Android – 在Facebook等通知栏中使用外部个人资料图片"

这个语句将使用一种方法将一个URL(自然是指向一个图像)转换成一个Bitmap

 Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/YOUR_USER_ID/picture?type=large"); 

注意:由于您提到了Facebook个人资料,因此我添加了一个可以获取Facebook用户的大尺寸个人资料照片的url。 但是,您可以将其更改为任何指向您需要在Notification显示的图片的url。

以及从上面的语句中指定的URL获取图像的方法:

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

现在将上面创build的bitmap实例传递给Notification.Builder实例。 我在这个示例代码中称它为builder 。 它用在这一行: builder.setLargeIcon(bitmap); 。 我假设你知道如何显示实际的Notification和它的configuration。 所以我会跳过这部分,只添加生成

 // CONSTRUCT THE NOTIFICATION DETAILS builder.setAutoCancel(true); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle("Some Title"); builder.setContentText("Some Content Text"); builder.setLargeIcon(bitmap); builder.setContentIntent(pendingIntent); 

哦,差点忘了,如果你还没有这样做的话,你需要在Manifest中设置这个权限:

 <uses-permission android:name="android.permission.INTERNET" /> 

在这里输入图像说明

首先使用下面的代码下载图片:

 private Bitmap getBitmap(String url) { File f=fileCache.getFile(url); //from SD cache Bitmap b = decodeFile(f); if(b!=null) return b; //from web try { Bitmap bitmap=null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is=conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); bitmap = decodeFile(f); return bitmap; } catch (Exception ex){ ex.printStackTrace(); return null; } } //decodes image and scales it to reduce memory consumption private Bitmap decodeFile(File f){ try { //decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f),null,o); //Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE=70; int width_tmp=o.outWidth, height_tmp=o.outHeight; int scale=1; while(true){ if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) break; width_tmp/=2; height_tmp/=2; scale*=2; } //decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize=scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) {} return null; } 

在下面的代码中使用该图像作为位图:

 Bitmap icon1 = downloadedBitmap; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setAutoCancel(true) .setContentTitle("DJ-Android notification") .setSmallIcon(R.drawable.ic_launcher) .setContentText("Hello World!"); NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle(); bigPicStyle.bigPicture(icon1); bigPicStyle.setBigContentTitle("Dhaval Sodha Parmar"); mBuilder.setStyle(bigPicStyle); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, testActivity.class); // The stack builder object will contain an artificial back stack // for // the // started Activity. // This ensures that navigating backward from the Activity leads out // of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(testActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(100, mBuilder.build()); 

我的事情你没有关于Android权限:

 <uses-permission android:name="android.permission.INTERNET" /> 

更详细的检查这个artical和android开发人员

我使用通用图像加载器来解决这个问题。 看看如何设置它的维基。 在你实例化一次后,这是我在我的GCM侦听器中使用的代码来下载和显示图像。 我下载了位图,然后将其设置在通知中:

 // Download profile picture of the user with Universal Image Loader Bitmap bitmap = ImageLoader.getInstance().loadImageSync(profilePhotoUrl); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_my_app) .setLargeIcon(bitmap) // This is the image displayed on the lock screen .setContentTitle("My App") .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); 
 Notification notif = new Notification.Builder(context) .setContentTitle("Title") .setContentText("content") .setSmallIcon(R.drawable.ic_small) .setLargeIcon(bitmap) .setStyle(new Notification.BigPictureStyle() .bigPicture(bigBitmap) .setBigContentTitle("big title")) .build();