Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the image in push notification (Gcm) android?

From two days I am trying to get like this. But I failed. Please any one suggest me how to get image in push notification. Thank you in advance.

enter image description here

Code what I tried:

@SuppressWarnings("deprecation")
private void handleMessage(Context mContext, Intent intent) {
    Bitmap remote_picture = null;
    long when = System.currentTimeMillis();
    int icon = R.drawable.reload_logo;
    try {
        Bundle gcmData = intent.getExtras();
        if(intent.getExtras().getString("message")!=null)
            Log.v("TAG_IMAGE", "" + intent.getExtras().getString("message"));
        Log.v("TAG_IMAGE", "" + intent.getExtras().getString("imageurl"));


        {
            NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
            notiStyle.setSummaryText(intent.getExtras().getString("message"));

            try {
                remote_picture = BitmapFactory.decodeStream((InputStream) new URL(intent.getExtras().getString("imageurl")).getContent());
            } catch (IOException e) {
                e.printStackTrace();
            }
            notiStyle.bigPicture(remote_picture);
            notificationManager = (NotificationManager) mContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent contentIntent = null;

            Intent gotoIntent = new Intent();
            gotoIntent.setClassName(mContext, "com.reloadapp.reload.fragments.MainActivity");//Start activity when user taps on notification.
            contentIntent = PendingIntent.getActivity(mContext,
                    (int) (Math.random() * 100), gotoIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    mContext);
            Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle("Reload.in")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message")))
                    .setContentIntent(contentIntent)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setLargeIcon(remote_picture)
                    .setContentText(intent.getExtras().getString("message"))
                    .setStyle(notiStyle).build();


            notification.flags = Notification.FLAG_AUTO_CANCEL;
            count++;
            notificationManager.notify(count, notification);//This will generate seperate notification each time server sends.
        }
    }catch (Throwable e) {
        e.printStackTrace();
    }
}

I am getting small icon and large icon are with same image. But I gave different.

result:

like image 883
kartheeki j Avatar asked Aug 13 '15 04:08

kartheeki j


People also ask

How to integrate GCM with Android push notifications?

Implement an Android Client app to register with GCM, send the registration id to your push notification server and manage the notifications sent from your server via GCM. Implement a server side API to get and store registration ids from the client app and optionally provide an Admin panel to send push notification from.

How do I set up push notifications on Android devices?

Get the SENDER_ID of the project or create a configuration file. Implement an Android Client app to register with GCM, send the registration id to your push notification server and manage the notifications sent from your server via GCM.

What is Google Cloud messaging notifications?

These Notifications looks as follow. GCM (Google cloud Messaging) is a free service provided by Google to send messages to different clients like (Android, iPhone, Chrome, Blackberry, etc). GCM delivers messages only when the client is online.

Is there any data limit in push notification?

There is data limit you can send in push notification. The message size limit in GCM is 4 kbytes. Instead of that you have to send Url of image in server then on Receiver side at time of receiving push notification you have fetch image from server and then you have to display image in notification.


1 Answers

thank you @Ravi.

@SuppressWarnings("deprecation")
    private void handleMessage(Context mContext, Intent intent) {
        Bitmap remote_picture = null;
        long when = System.currentTimeMillis();
        int icon = R.drawable.reload_logo;
        Bundle gcmData = intent.getExtras();
        //if message and image url
        if(intent.getExtras().getString("message")!=null && intent.getExtras().getString("imageurl")!=null) {
            try {


                Log.v("TAG_IMAGE", "" + intent.getExtras().getString("message"));
                Log.v("TAG_IMAGE", "" + intent.getExtras().getString("imageurl"));


                NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
                notiStyle.setSummaryText(intent.getExtras().getString("message"));

                try {
                    remote_picture = BitmapFactory.decodeStream((InputStream) new URL(intent.getExtras().getString("imageurl")).getContent());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                notiStyle.bigPicture(remote_picture);
                notificationManager = (NotificationManager) mContext
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                PendingIntent contentIntent = null;

                Intent gotoIntent = new Intent();
                gotoIntent.setClassName(mContext, "com.reloadapp.reload.fragments.MainActivity");//Start activity when user taps on notification.
                contentIntent = PendingIntent.getActivity(mContext,
                        (int) (Math.random() * 100), gotoIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        mContext);
                Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0)
                        .setAutoCancel(true)
                        .setContentTitle("Reload.in")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message")))
                        .setContentIntent(contentIntent)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

                        .setContentText(intent.getExtras().getString("message"))
                        .setStyle(notiStyle).build();


                notification.flags = Notification.FLAG_AUTO_CANCEL;
                count++;
                notificationManager.notify(count, notification);//This will generate seperate notification each time server sends.

            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
}

for multi line:

 .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message"))) // use this
like image 147
kartheeki j Avatar answered Sep 18 '22 09:09

kartheeki j