服务中未显示 android 通知

标签 android service push-notification background-thread

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

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 的代码粘贴到主要 Activity 的 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);
    }

而且有效。

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

P.S: the service will run even if the app is not running

最佳答案

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

new Thread(new Runnable() { @Override public void run() { //fetching notifications from server //if there is notifications then call this method ShowNotification(); } }).start();

并且您必须在 AndroidManifest.Xml 中注册服务</application> 之前的文件.

<service android:name=".NotificationService"/>

之后,您必须按照以下方式从您的 Activity 开始服务。

startService(new Intent(this,NotificationService.class));

有你的服务代码:

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

}

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

enter image description here

关于服务中未显示 android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999945/

相关文章:

java - 在 Android 中设置微调器 onClickListener()

linux - 如何在Linux中执行cron服务?

android - 启动后调试服务

ios - 如果用户不单击通知,则在后台快速通知更新应用程序

ios - 锁定屏幕后,“首次推送”通知消失

ios - 是否可以在 Xcode 中使用临时配置文件调试我的应用程序?

android - Espresso 测试无法在某些设备上单击 DialogBu​​tton

java - 将 charSequence 元素转换为 enum 元素

android - 如何使用 APK 访问 Android STK 菜单?

angular - 测试订阅服务