B.T
B.T

Reputation: 551

Notification `notify()` cannot be referenced from a static context

I'm using a simple code to push notifications in Android through a function. This function is the following :

public void sendNotification(View view) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("My notification").setContentText("Hello World!");

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());
    }}

This is example function i picked on a website.

Everything is ok, the NotificationCompat.builder doesn't return any error. But the first notify() on the last line return the following error : Non-static method 'notify()' cannot be referenced from a static context.

I really don't understand why. The void is placed in my MainActivity.javainside my public class MainActivity extends AppCompatActivity {}

EDIT :

The solution was to remove NotificationManager.notify(). from mNotificationManager.notify(001, mBuilder.build());

Upvotes: 1

Views: 867

Answers (2)

Vasant
Vasant

Reputation: 3585

Please see below solution.

public void sendNotification(View view) {

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("My notification").setContentText("Hello World!");

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(001, mBuilder.build());
}}

Upvotes: 2

AskNilesh
AskNilesh

Reputation: 69734

Use this

mNotificationManager.notify(001, mBuilder.build());

Instead of this

NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());

Upvotes: 1

Related Questions