如何在推送通知(Gcm)android中显示图像? [英] How to show the image in push notification (Gcm) android?

查看:180
本文介绍了如何在推送通知(Gcm)android中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从两天开始,我试图得到这样的结果。但我失败了。请任何人建议我如何在推送通知中获取图像。预先感谢您。





代码我试过:

$ pre $ @ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' b位图remote_picture = null;
long when = System.currentTimeMillis();
int icon = R.drawable.reload_logo;
尝试{
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)); $(b)b
尝试{
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); //当用户点击通知时开始活动。
contentIntent = PendingIntent.getActivity(mContext,
(int)(Math.random()* 100),gotoIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
通知通知= 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); //这将在每次服务器发送时生成单独的通知。
}
} catch(Throwable e){
e.printStackTrace();


我越来越小图标和大图标也一样图片。但我给了他们不同的结果。



结果:

解决方案

Ravi。

  @SuppressWarnings(deprecation)
private void handleMessage(Context mContext,Intent intent){
位图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)); $(b)b
尝试{
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); //当用户点击通知时开始活动。
contentIntent = PendingIntent.getActivity(mContext,
(int)(Math.random()* 100),gotoIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
通知通知= 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); //这将在每次服务器发送时生成单独的通知。

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

p>

  .setStyle(new NotificationCompat.BigTextStyle()。bigText(intent.getExtras()。getString(message)))//使用这个


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.

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:

解决方案

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

这篇关于如何在推送通知(Gcm)android中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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