Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android通知未从服务中显示_Android_Service_Push Notification_Background Thread - Fatal编程技术网

android通知未从服务中显示

android通知未从服务中显示,android,service,push-notification,background-thread,Android,Service,Push Notification,Background Thread,我有这个服务,它将连接到服务器并获取通知 但不幸的是,它没有显示任何通知 这是服务类: public class NotificationService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } public void startNotificationListener() { /

我有这个服务,它将连接到服务器并获取通知 但不幸的是,它没有显示任何通知 这是服务类:

public class NotificationService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void startNotificationListener()
    {
      //start's a new thread
    new Thread(new Runnable() {
        @Override
        public void run() {
          //fetching notifications from server
         //if there is notifications then call this method
        ShowNotification();
        }
    }).start();
    @Override
    public void onCreate()
    {
        startNotificationListener();
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent,int flags,int startId)
    {
        return super.onStartCommand(intent,flags,startId);
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
    public void ShowNotification()
    {
      NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
            Notification notification = new NotificationCompat.Builder(getBaseContext(),"notification_id")
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("title")
                    .setContentText("content")
                    .setDefaults(NotificationCompat.DEFAULT_SOUND)
                    .build();
            notificationManager.notify(0, notification);
       //the notification is not showing

    }
}
调用ShowNotification时通知没有显示,我已经尝试将ShowNotification的代码粘贴到主活动的oncreate中,就像这样

@Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
          NotificationManager notificationManager =
                (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(getBaseContext(),"n")
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("Title")
                .setContentText("Content Text"))
                .setDefaults(NotificationCompat.DEFAULT_SOUND)
                .build();
        notificationManager.notify(0, notification);
    }
它是有效的

我是否总是需要从活动中创建通知? 如果没有,如何从服务创建通知

附言:即使应用程序没有运行,服务也会运行


是的,您可以从服务创建通知。但是根据您的代码,
startNotificationListener()
的右括号在下面的代码后面缺失

new线程(new Runnable()){
@凌驾
公开募捐{
//从服务器获取通知
//如果有通知,则调用此方法
ShowNotification();
}
}).start()

之前,您必须在
AndroidManifest.Xml
文件中注册服务

<service android:name=".NotificationService"/>
您的服务代码如下:

public class NotificationService extends Service {

public void startNotificationListener() {
    //start's a new thread
    new Thread(new Runnable() {
        @Override
        public void run() {
            //fetching notifications from server
            //if there is notifications then call this method
            ShowNotification();
        }
    }).start();
}
@Override
public void onCreate()
{
    startNotificationListener();
    super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
    return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy()
{
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void ShowNotification()
{
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(getBaseContext(),"notification_id")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("title")
            .setContentText("content")
            .setDefaults(NotificationCompat.DEFAULT_SOUND)
            .build();
    notificationManager.notify(0, notification);
    //the notification is not showing

}
}

您将收到通知。请检查屏幕截图


尝试使用通知频道……可能就是这个问题

从Android 8.0(API级别26)开始,所有通知都必须 指定给频道,否则将不显示


您从未在
通知服务
的任何位置调用
启动通知服务()。您确实调用了
startNotificationListener()
,但该方法似乎不存在。很抱歉,该方法是startNotificationListener,这是一个错误。编辑了该问题。请再次编辑以包含一个问题。当前编辑仍然不会编译;e、 例如,任何地方都没有声明
args
。args只是从获取过程中传递的参数。它们包含通知信息。您可以在没有args的情况下尝试,问题是通知不会显示,无论我在服务中如何使用它Hello Patrik,感谢您抽出时间让服务工作,我在这里重写代码时犯了一些错误,但在我的项目中,代码没有问题,因为我使用日志,它显示服务正在运行,但无论我做什么,通知都不会显示在Kitkat(我现在使用的手机)上,我在模拟器(Oreo)上试过,它在那里不起作用,谢谢你的时间,但据我所知,这些频道在Kitkat上是不必要的,顺便说一句,我以前确实使用过它们,但它不起作用
public class NotificationService extends Service {

public void startNotificationListener() {
    //start's a new thread
    new Thread(new Runnable() {
        @Override
        public void run() {
            //fetching notifications from server
            //if there is notifications then call this method
            ShowNotification();
        }
    }).start();
}
@Override
public void onCreate()
{
    startNotificationListener();
    super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
    return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy()
{
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void ShowNotification()
{
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(getBaseContext(),"notification_id")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("title")
            .setContentText("content")
            .setDefaults(NotificationCompat.DEFAULT_SOUND)
            .build();
    notificationManager.notify(0, notification);
    //the notification is not showing

}
        // The id of the channel.
        String CHANNEL_ID = "my_channel_01";
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MainActivity.this).setChannel(CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MainActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your app to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // mNotificationId is a unique integer your app uses to identify the
        // notification. For example, to cancel the notification, you can pass its ID
        // number to NotificationManager.cancel().
        mNotificationManager.notify(0, mBuilder.build());