Search code examples
javaandroidnotificationsnotificationmanager

Can't create notification even though notification manager is initialized correctly


I am trying to send a notification using the NotificationManager API in Android. Below is my code:

AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="23"
    android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
    <activity
        android:name="com.tabs.activity.Comments"
        android:configChanges="orientation|keyboardHidden"
        android:label="View Post"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateAlwaysHidden|adjustResize" >
    </activity>
    <service
        android:name="com.tabs.activity.NotificationService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
...

Comments.java:

public void createComment(){
    ...
    NotificationService notificationService = new NotificationService();
    notificationService.showNotification(getApplicationContext(), R.id.commenter_profile_photo, "User commented: Hi!", "User");
    ...
}

NotificationService.java:

public void showNotification(Context context, int commenterId, String messageBody, String commenter){
    NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Title")
            .setContentText(messageBody)
            .setTicker("New comment from " + commenter)
            .setSmallIcon(commenterId);

    Intent moreInfoIntent = new Intent(context, news_feed.class);

    //When the user clicks back, it doesn't look sloppy!
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addNextIntent(moreInfoIntent);

    //If the intent already exists, just update it and not create a new one
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    //When the notification is actually clicked on
    notifiationBuilder.setContentIntent(pendingIntent);

    //Notification manager to notify of background event
    notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(1, notifiationBuilder.build());
}

Error that is shown (Stack Trace):

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                          at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:160)
                                                          at android.app.Notification$Builder.<init>(Notification.java:2287)
                                                          at android.support.v4.app.NotificationCompatApi21$Builder.<init>(NotificationCompatApi21.java:68)
                                                          at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:759)
                                                          at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1559)
                                                          at com.tabs.activity.NotificationService.showNotification(NotificationService.java:113)
                                                          at com.tabs.activity.Comments.createComment(Comments.java:146)

The problem with this code is that for whatever reason, the notificationManager.notify(1, notifiationBuilder.build()); line always produces a NullPointerException. I know what they are but I can't seem to solve this one because I don't understand why my context would need getApplicationInfo(). My context should already have that when I pass it in from Comments.java which is confusing me. I just don't see how I could have built the notification manager incorrectly. Below is the stack trace that is given. Any help would be appreciated. Thanks!


Solution

  • Change

    NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(this)
    

    to

    NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(context)