2

I am new to Android Programming and I am trying to set a Push notification with a big image along with the text and icon. But I am getting an error doing so. I have been trying to resolve it for hours now but can't. Help me if anybody knows how to do it.

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

This is the URL in the "imageurl" field in the intent : "http://omitra.in/demo/logo.png"

This is the error I get :

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) 
curiousMind
  • 2,812
  • 1
  • 17
  • 38
  • Possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Sufian Dec 21 '15 at 08:04
  • Please always Google the logcat errors before posting questions. As you can see this problem has been fixed before in another SO question. – Sufian Dec 21 '15 at 08:05

1 Answers1

2

Caused by: android.os.NetworkOnMainThreadException

It means that you tried to visit internet in main thread.

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

You may need put this code in a thread

Cook Chen
  • 527
  • 3
  • 11
  • I have now created a new thread and notification is also showing up. But it doesn't show that big image in it. It just shows the icon, title and text nothing else. I have updated my code now. Please check where is the mistake. – Sybil Naushad Dec 21 '15 at 07:43