android - 如何在推送通知(Gcm)android 中显示图像?

标签 android push-notification google-cloud-messaging

从这两天开始,我一直在努力做到这一点。但是我失败了。请任何人建议我如何在推送通知中获取图像。提前谢谢你。

enter image description here

编写我尝试过的代码:

@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();
    }
}

我得到小图标和大图标具有相同的图像。但是我给的不一样。

结果:

最佳答案

谢谢@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();
            }
        }
}

对于多行:

 .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message"))) // use this

关于android - 如何在推送通知(Gcm)android 中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979394/

相关文章:

android - 如何在 gradle 任务中检索库版本

android - 如何在安卓软键盘中替换表情符号

android - 会说话的应用程序就像会说话的汤姆猫一样,录音在所有设备上都不起作用

ios - 我的 iPhone 应用程序可以注册以接收其他应用程序的推送通知吗?

javascript - 将通知导航到它属于 react-native 的屏幕

java - 加载图像时应用程序崩溃

objective-c - 在 iOS 中没有报摊的应用程序处于非事件状态时在后台下载文件

android - GCM(现在的 FCM)是否有任何限制?

javascript - 谷歌浏览器推送通知

安卓 GCM onMessage