首页 > 解决方案 > 不能从静态上下文引用通知`notify()`

问题描述

我正在使用一个简单的代码通过一个函数在 Android 中推送通知。该功能如下:

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());
    }}

这是我在网站上选择的示例功能。

一切正常,NotificationCompat.builder不返回任何错误。但是notify()最后一行的第一个返回以下错误:Non-static method 'notify()' cannot be referenced from a static context

我真的不明白为什么。虚无被放置在我的MainActivity.java内心public class MainActivity extends AppCompatActivity {}

编辑 :

解决方案是NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());

标签: androidandroid-notifications

解决方案


请参阅下面的解决方案。

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());
}}

推荐阅读