推送通知中未显示图像

Posted

技术标签:

【中文标题】推送通知中未显示图像【英文标题】:Image not shown in Push Notification 【发布时间】:2015-12-21 07:16:36 【问题描述】:

我是 android 编程新手,我正在尝试设置带有大图像以及文本和图标的推送通知。但是我这样做会出错。我一直试图解决它几个小时,但不能。如果有人知道该怎么做,请帮助我。

NotificationCompat.Builder notification=new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
notification.setContentTitle("This is a notification");
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setContentText("This is a sample notification text");
notification.setWhen(System.currentTimeMillis());

//Bitmap remote_picture = null;  ---> this is now my global variable.
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");
new Thread(new Runnable() 
        @Override
        public void run() 
            try
            
                remote_picture = BitmapFactory.decodeStream((InputStream) new URL(getIntent().getExtras().getString("imageurl")).getContent());
            
            catch (IOException e)
            
                e.printStackTrace();
            
        
    ).start();
    notiStyle.bigPicture(remote_picture);

notification.setStyle(notiStyle);

Intent intent=new Intent(this,sample.class);
intent.putExtra("temp",temp);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
notification.setContentIntent(pendingIntent);

NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id,notification.build());

这是意图中“imageurl”字段中的 URL:“http://omitra.in/demo/logo.png”

这是我得到的错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:4446)
at android.view.View$PerformClick.run(View.java:18437)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4446) 
at android.view.View$PerformClick.run(View.java:18437) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5372) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at java.net.URLConnection.getContent(URLConnection.java:190)
at java.net.URL.getContent(URL.java:455)
at com.example.sachinparashar.omitravendorassistance.Verify.defineNotification(Verify.java:135)
at com.example.sachinparashar.omitravendorassistance.Verify.onVerify(Verify.java:90)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:4446) 
at android.view.View$PerformClick.run(View.java:18437) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5372) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 
at dalvik.system.NativeStart.main(Native Method) 

【问题讨论】:

android.os.NetworkOnMainThreadException的可能重复 在发布问题之前,请始终使用 Google 搜索 logcat 错误。如您所见,此问题之前已在另一个 SO 问题中得到解决。 【参考方案1】:

原因:android.os.NetworkOnMainThreadException

这意味着你试图在主线程中访问互联网。

remote_picture = BitmapFactory.decodeStream((InputStream) new URL(getIntent().getExtras().getString("imageurl")).getContent());

您可能需要将此代码放在一个线程中

【讨论】:

我现在创建了一个新线程并且通知也显示出来了。但它并没有显示出那么大的形象。它只显示图标、标题和文本。我现在更新了我的代码。请检查错误在哪里。

以上是关于推送通知中未显示图像的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 8.0 中未显示使用 parse sdk 的推送通知

推送通知在 ios 9 中未显示 2kb

在科尔多瓦项目中未收到解析推送通知

在android中未收到推送通知

在 iOS 中未收到推送通知消息

在 GCM 中未收到推送通知