title: How to implement notification in a Service?
body: <p>I am trying to send a notification when I receive a particular alarm in my Alarm Receiver class which then sends a notification to another Service running in the background. </p>
  
  <p>I am getting this error:</p>
  
  <pre><code>Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
  </code></pre>
  
  <p>I have researched a lot on this topic but none of the solutions that I found have worked.</p>
  
  <p>This is <strong>Alarm Receiver Class</strong></p>
  
  <pre><code>public void onReceive(Context context, Intent intent)
      {
          switch(intent.getIntExtra("DosageIdentifier",-1)) {
              case 1:
              {
                  for(int i = 0; i &lt; SetMyAlarm.countSeqWithDatabaseList.size(); i++)
                  {
                      prescriptionString = SetMyAlarm.prescriptionList.get(i);
                      myNotification.acceptNotificationString(prescriptionString);
  
                  }
                  break;
              }
  </code></pre>
  
  <p>This is my <strong>SetMyAlarm Service</strong></p>
  
  <pre><code>public void acceptNotificationString(String text)
      {
          sendNotification(text);
      }
      private void sendNotification(String text) {
          try {
              Intent notificationIntent = new Intent(this, MainActivity.class);
              notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
              int flags = PendingIntent.FLAG_UPDATE_CURRENT;
              PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, flags);
  
              int icon = R.mipmap.ic_launcher;
              CharSequence tickerText = "ALERT";
              CharSequence contentTitle = "Notification";
              CharSequence contentText = text;
  
              Notification notification = new Notification.Builder(this)
                      .setSmallIcon(icon)
                      .setTicker(tickerText)
                      .setContentTitle(contentTitle)
                      .setContentText(contentText)
                      .setContentIntent(pendingIntent)
                      .setAutoCancel(true)
                      .build();
  
              NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
              final int NOTIFICATION_ID = 1;
              manager.notify(NOTIFICATION_ID, notification);
          }
          catch (NullPointerException e)
          {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p><strong>This is the error I am getting:</strong></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.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
      at android.content.ComponentName.&lt;init&gt;(ComponentName.java:128)
      at android.content.Intent.&lt;init&gt;(Intent.java:4449)
      at timertesting.kaad.timertesting.SetMyAlarm.sendNotification(SetMyAlarm.java:564)
      at timertesting.kaad.timertesting.SetMyAlarm.acceptNotificationString(SetMyAlarm.java:560)
      at timertesting.kaad.timertesting.AlarmReceiver.onReceive(AlarmReceiver.java:23)
      at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
      at android.app.ActivityThread.-wrap14(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>Any Suggestions?</p>