title: NotificationManager Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
body: <p>In dev environment I have no problem with creating notification channel. But in qa/prod release creating notification channel causes following exception: </p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
      at android.app.NotificationManager.createNotificationChannels(NotificationManager.java:488)
      at android.app.NotificationManager.createNotificationChannel(NotificationManager.java:476)
      at net.ugbb.***.service.FirebaseMessagingService.createNotificationChannel(Unknown Source:53)
      at net.ugbb.***.service.FirebaseMessagingService.onMessageReceived(Unknown Source:38)
      at com.google.firebase.messaging.FirebaseMessagingService.zzl(Unknown Source:47)
      at com.google.firebase.messaging.FirebaseMessagingService.zzJ(Unknown Source:96)
      at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:65)
      at com.google.firebase.iid.zzb$zza$1.run(Unknown Source:24)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
      at java.lang.Thread.run(Thread.java:784)
  </code></pre>
  
  <p>Here is code snippert for creating NotificationChannel</p>
  
  <pre><code>private void createNotificationChannel() {
      // Create the NotificationChannel, but only on API 26+ because
      // the NotificationChannel class is new and not in the support library
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
          CharSequence name = getBaseContext() != null ? getString(R.string.channel_name) : "";
          String description = getBaseContext() != null ? getString(R.string.channel_description) : "";
          int importance = NotificationManager.IMPORTANCE_DEFAULT;
          NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
          channel.setDescription(description);
          // Register the channel with the system; you can't change the importance
          // or other notification behaviors after this
          NotificationManager notificationManager = getSystemService(NotificationManager.class);
          if (notificationManager != null)
              if (getApplicationContext() != null &amp;&amp; getApplicationContext().getPackageName() != null)
                  notificationManager.createNotificationChannel(channel);
      }
  }
  </code></pre>
  
  <p>We are using IBM Maas360 MDM for wrapping our app and distributed to our enterprise customers.</p>
  
  <p>Is there any solution how we can solve this problem or at least workaround?</p>
  
  <p>Update: </p>
  
  <p>Code for onMessageReceived</p>
  
  <pre><code>@Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
  
      PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
  
      if (!(powerManager != null &amp;&amp; powerManager.isInteractive())) {
          // TODO check documentation for changing deprecated constant FULL_WAKE_LOCK
          PowerManager.WakeLock wakeLock = powerManager != null ? powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "fiona:myLock") : null;
          if (wakeLock != null) {
              wakeLock.acquire(10*60*1000L /*10 minutes*/);
          }
      }
  
      sendNotification(remoteMessage.getData());
  }
  
  private void sendNotification(Map&lt;String, String&gt; data) {
      Intent intent = null;
      if (null != data) {
          String dataType;
          if (data.containsKey(ENotifElements.DATA_TYPE_KEY.value()))
              dataType = data.get(ENotifElements.DATA_TYPE_KEY.value());
          else
              return;
  
          switch (dataType) {
              case DIGITALLIZATION_EQUIPMENT:
                  intent = handleDigitalizationProvisioningNotification(data);
                  break;
              case NOTIF_TYPE_PROVISIONING:
                  intent = handleDeviceProvisioningNotification(data);
                  break;
              case TASK_LIST_CHANGED:
                  intent = handleTaskListChangedNotification(data);
                  break;
              case SIGNED_DOCUMENT_COMPLETION:
                  intent = signedDocumentCompletion(data);
                  break;
              case DIGITALIZATION_TASK_COMPLETE:
                  intent = digitalizationTaskComplete(data);
                  break;
              case SALES:
                  salesNotification(data);
                  break;
              default:
                  intent = new Intent(this, WorkordersActivity.class);
                  break;
          }
  
      }
  
      // Form notification item
      prepareNotificationContent(intent, data);
  }
  
      private void prepareNotificationContent(Intent intent, Map&lt;String, String&gt; data) {
      if (intent != null)
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
      PendingIntent pendingIntent = PendingIntent.getActivity(this, UNIQUE_INT_PER_CALL++, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  
      if(data !=null) {
          if (data.containsKey(ENotifElements.DATA_TYPE_KEY.value())) {
              String dataType = data.get(ENotifElements.DATA_TYPE_KEY.value());
              if (dataType.equals(SALES)) {
                  pendingIntent = PendingIntent.getActivity(this, UNIQUE_INT_PER_CALL++, intent, PendingIntent.FLAG_ONE_SHOT);
                  Log.i(TAG, "sendNotification: SALES INTENT");
              }
          }
      }
  
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FirebaseIdService.CHANNEL_ID)
              .setSmallIcon(R.drawable.ic_notifications_black_24dp)
              .setContentTitle(data != null ? data.get(ENotifElements.TITLE_KEY.value()) : "ERROR")
              .setContentText(data != null ? data.get(ENotifElements.TEXT_KEY.value()) : "ERROR")
              .setAutoCancel(true)
              .setSound(defaultSoundUri)
              .setVibrate(new long[]{1000, 1000, 1000})
              .setContentIntent(pendingIntent);
  
      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
      Objects.requireNonNull(notificationManager).notify(new Random().nextInt(), notificationBuilder.build());
  }
  </code></pre>