1

I want to send push notification to android application with icon from the server side. Is it possible or did i get it wrong? enter image description here If it's possible, then which image format are expected as an input for PyFCM method notify_single_device for parameter message_icon. Didn't get the answer from the source code in github.

It's just referred as an variable. Base64 doesn't go through.

jaanus
  • 404
  • 5
  • 14
  • 1
    You need to make your question clearer, do you want to set a notification icon drawable from a remote resource or you want to show a notification with an image attachment? – olucurious Apr 11 '17 at 18:40
  • 1
    I found the answer myself from [FCM docs](https://firebase.google.com/docs/cloud-messaging/concept-options). `A notification message is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. Data messages let developers send up to 4KB of custom key-value pairs.` Image isn't key-value pair and having image smaller than 4kb is pretty useless which means its not possible to send push notification icon from the server side. – jaanus Apr 17 '17 at 11:20

1 Answers1

2

You can attach the url of the image to the message data payload in pyfcm:

data_message = {
    "icon_url" : "http//...."
}
push_service.notify_single_device(registration_id=registration_id, 
message_body=message_body, data_message=data_message)

And get the "icon_url" in your Android app and fetch it as Bitmap resource with:

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

then use setLargeIcon (Bitmap icon) of NotificationCompat.Builder to set the image as the notification icon

olucurious
  • 136
  • 1
  • 2
  • 5