简体   繁体   English

Android前台服务通知未显示在状态栏中

[英]Android foreground service notification not showing in status bar

I have tried various solution here on SO and can't seem to get any of them to work. 我在SO上尝试了各种解决方案,但似乎无法使它们中的任何一个起作用。 The notification simply will not show. 该通知根本不会显示。 Can anyone point out where possible issues in my code could be? 谁能指出我的代码中可能存在的问题?

private void startRunningInForeground() {

        setupNotificationChannel();

        Intent showTaskIntent = new Intent(getApplicationContext(), MainActivity.class);
        showTaskIntent.setAction(Intent.ACTION_MAIN);
        showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                showTaskIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, SERVICE_NOTIFICATION_CHANNEL);
        builder.setSmallIcon(R.drawable.ic_stat_ic_logo);
        builder.setContentTitle(getString(R.string.merge_notif_title));
        builder.setContentText(getString(R.string.merge_notif_description));
        builder.setContentIntent(contentIntent);
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(false);
        builder.setOngoing(true);

        startForeground(NOTIFICATION_ID, builder.build());
    }

    private void setupNotificationChannel() {
        // Only run this on versions of Android that require notification channels.
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return;
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if(notificationManager == null) {
            return;
        }

        NotificationChannel cachingChannel = new NotificationChannel(SERVICE_NOTIFICATION_CHANNEL,
                getString(R.string.merge_channel), NotificationManager.IMPORTANCE_HIGH);
        cachingChannel.setDescription(getString(R.string.merge_channel_description));
        cachingChannel.enableLights(false);
        cachingChannel.enableVibration(false);

        notificationManager.createNotificationChannel(cachingChannel);
    }  

Not receiving any any error messages and the notification channel is being created because I see it in the app settings. 没有收到任何错误消息,并且正在创建通知通道,因为我在应用程序设置中看到了它。

What does your startForeground do? 您的startForeground是做什么的?

try modify this and make sure your os is > Android 8.0 尝试修改它,并确保您的操作系统是> Android 8.0

btw the Channel only needs to be created once. 顺便说一句,通道只需要创建一次。

PendingIntent contentIntent = PendingIntent.getActivity(
            getApplicationContext(),
            NOTIFICATION_ID, <---
            showTaskIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);


startForeground(NOTIFICATION_ID, builder.build());
notificationManager.notify(NOTIFICATION_ID, builder.build()); <--

Maybe because of Android O bg service restrictions 可能是由于Android O bg服务限制

//Start service:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  startForegroundService(new Intent(this, YourService.class));
} else {
  startService(new Intent(this, YourService.class));
}

So I was able to get it to work by extending Service instead of IntentService. 因此,我可以通过扩展Service而不是IntentService使其工作。 I cannot give an explanation as to why this worked, but everything is now working as expected. 我无法解释为什么这样做,但是现在一切都按预期进行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM