Android的 - 使用外部轮廓图像,如Facebook通知栏 [英] Android - use external profile image in notification bar like Facebook

查看:167
本文介绍了Android的 - 使用外部轮廓图像,如Facebook通知栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以在推送通知参数,如消息,标题,图片的URL等如何Facebook的显示您的个人资料PIC与你在通知区域信息发送信息?我想用外部图像在通知区域,所以当你拉下来,你看到个人资料图片与消息。现在,我的只显示了绘制文件夹的默认图标。我想这可能是一个共同的问题,但没有找到任何东西。任何帮助将是很好。

I know you can send info in the push notification parameters like message, title, image URL, etc. How does Facebook show your profile pic with your message in the notification area? I want to use an external image in the notification area, so when you pull it down you see the profile image with the message. Right now, mine just shows the default icon from the drawable folder. I figured this might be a common question but couldn't find anything. Any help would be nice.

推荐答案

这语句将使用一种方法来转换一个URL(当然,一个指向图像)转换成位图

This statement will use a method to convert a URL (naturally, one that points to an image) into a Bitmap.

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

注意:既然你提到一个Facebook的个人资料,我已经包含了获取的Facebook用户的大尺寸个人资料图片的URL。您可以但是,这种变化为指向,你需要在通知显示图像的任何URL

Note: Since you mention a Facebook profile, I have included an URL that gets your the large size profile picture of a Facebook User. You can however, change this to any URL that points to an image that you need to display in the Notification.

这会得到你在上面的语句中指定的URL的图像方式:

And the method that will get the image from the URL you specified in the statement above:

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

现在通过位以上的 Notification.Builder 实例创建实例。我把它在这个例子中code 建设者 。这是用在该行: builder.setLargeIcon(位); 。我假设你知道如何来显示实际通知和它的配置。因此,我将跳过这一部分,并添加在建设者的。

Now pass the bitmap instance created above to the Notification.Builder instance. I call it builder in this example code. It is used in this line: builder.setLargeIcon(bitmap);. I am assuming you know how to display the actual Notification and it's configurations. So I will skip that part and add just the builder.

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

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

Oh, almost forgot, if you haven't already done so, you will need this permission setup in the Manifest:

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

这篇关于Android的 - 使用外部轮廓图像,如Facebook通知栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆