Android Notification crashes my app
<p>I have an android app which crashes when it receives notification. The notification receive messages from my app server.Tapping the notification opens up a webview activity. My problem is when I hardcode an url link to open in the webview, it works fine. But, when I use a URL link (in string value) sent from my app server and save it in shared preferences and then use it to start the webview activity the app crashes.</p>

<p>The code is as follows:-</p>

<p>This is the GCMPushReceiverClass where I receive messages and send notification:-</p>

<pre><code>public class GCMPushReceiverService extends GcmListenerService {

     Context applicationContent = FirstActivity.getApplicationData();

     @Override
     public void onMessageReceived(String from, Bundle data) {
         String message = data.getString("message");
         String link = data.getString("link");
         sendNotification(message);
         SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContent);
         SharedPreferences.Editor writer = mPrefs.edit();
         writer.putString("LINK",link);
         writer.commit();
     }

     private void sendNotification(String message) {
           Intent intent = new Intent(this, ThirdActivity.class);
           intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           int requestCode = 0;
           Random random = new Random();
           int m = random.nextInt(9999-1000)+1000;
           PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
           int color = getResources().getColor(R.color.notColour);
           NotificationCompat.Builder notBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                 .setSmallIcon(R.drawable.sovran_icon)
                 .setDefaults(Notification.DEFAULT_ALL)
                 .setPriority(1)
                 .setLargeIcon(BitmapFactory.decodeResource(getBaseContext().getResources(), R.mipmap.ic_launcher))
                 .setColor(color)
                 .setContentTitle("New Message")
                 .setContentText(message)
                 .setAutoCancel(true)
                 .setContentIntent(pendingIntent);

            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(m, notBuilder.build());
      }
}
</code></pre>

<p>This is my webview activity named ThirdActivity which receives the intent from notifications and starts the webview activity</p>

<pre><code>public class ThirdActivity extends AppCompatActivity {

     private WebView mWebView;


     Context applicationContent = FirstActivity.getApplicationData();
     SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContent);
     String link = mPrefs.getString("LINK","");

     public class mWebViewClient extends WebViewClient {

          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
               view.loadUrl(url);
               return false;
          }
     }


     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_third);

          mWebView = (WebView) findViewById(R.id.webView1);
          mWebView.setWebViewClient(new mWebViewClient());
          mWebView.getSettings().setJavaScriptEnabled(true);
          mWebView.getSettings().setBuiltInZoomControls(true);
          mWebView.loadUrl(link);
     }
}
</code></pre>

<p>This is the crash report</p>

<pre><code>&gt; 07-01 14:05:57.101 2370-2423/com.sovraan E/AndroidRuntime: FATAL
&gt; EXCEPTION: AsyncTask #1
&gt;                                                            Process: com.sovraan, PID: 2370
&gt;                                                            java.lang.NullPointerException: Attempt to invoke virtual method
&gt; 'java.lang.String android.content.Context.getPackageName()' on a null
&gt; object reference
&gt;                                                                at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
&gt;                                                                at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
&gt;                                                                at com.sovraan.GCMPushReceiverService.onMessageReceived(GCMPushReceiverService.java:30)
&gt;                                                                at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
&gt;                                                                at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
&gt;                                                                at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
&gt;                                                                at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
&gt;                                                                at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
&gt;                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
&gt;                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
&gt;                                                                at java.lang.Thread.run(Thread.java:818)
</code></pre>

<p><strong>One important thing to note</strong></p>

<p>The crash happens only if the app is not running.</p>

<p>If I start the app before any notification is received then notifications are received, the app works fine. Again if I kill the app , the same crash happens when I receive notifications. </p>