Android - use external profile image in notification bar like Facebook

后端 未结 5 1489
眼角桃花
眼角桃花 2020-11-30 06:37

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 notificati

5条回答
  •  情歌与酒
    2020-11-30 07:25

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

    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.

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

    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:

    
    

提交回复
热议问题