Android - 在通知栏中使用外部个人资料图片,如 Facebook [英] Android - use external profile image in notification bar like Facebook

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

问题描述

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

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(自然是指向图像的 URL)转换为 Bitmap.>

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 个人资料,因此我提供了一个 URL,可以获取您的 Facebook 用户的大尺寸个人资料图片.但是,您可以将其更改为指向您需要在 Notification 中显示的图像的任何 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;
    }
}

现在将上面创建的 bitmap 实例传递给 Notification.Builder 实例.在此示例代码中,我将其称为 builder.它在这一行中使用:builder.setLargeIcon(bitmap);.我假设您知道如何显示实际的 Notification 及其配置.所以我会跳过那部分,只添加 builder.

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天全站免登陆