Android foreground service notification not showing

前端 未结 6 2146
太阳男子
太阳男子 2020-12-01 10:53

I am trying to start a foreground service. I get notified that the service does start but the notification always gets suppressed. I double checked that the app is allowed t

相关标签:
6条回答
  • 2020-12-01 11:12

    if you are targeting Android 9(Pie) api level 28 and higher than you should give FOREGROUND_SERVICE permission in manifest file.see this link : https://developer.android.com/about/versions/pie/android-9.0-migration#bfa

    0 讨论(0)
  • 2020-12-01 11:15

    If none of the above worked you should check if your notification id is 0 ... SURPRISE!! it cannot be 0.

    Many thanks to @Luka Kama for this post

    startForeground(0, notification); // Doesn't work...
    
    startForeground(1, notification); // Works!!!
    
    0 讨论(0)
  • 2020-12-01 11:15

    For me everything was set correctly (also added FOREGROUND_SERVICE permission to manifest), but I just needed to uninstall the app and reinstall it.

    0 讨论(0)
  • 2020-12-01 11:25

    It's because of Android O bg services restrictions.

    So now you need to call startForeground() only for services that were started with startForegroundService() and call it in first 5 seconds after service has been started.

    Here is the guide - https://developer.android.com/about/versions/oreo/background#services

    Like this:

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

    Then create and show notification (with channel as supposed earlier):

    private void createAndShowForegroundNotification(Service yourService, int notificationId) {
    
        final NotificationCompat.Builder builder = getNotificationBuilder(yourService,
             "com.example.your_app.notification.CHANNEL_ID_FOREGROUND", // Channel id
        NotificationManagerCompat.IMPORTANCE_LOW); //Low importance prevent visual appearance for this notification channel on top 
        builder.setOngoing(true)
        .setSmallIcon(R.drawable.small_icon)
        .setContentTitle(yourService.getString(R.string.title))
        .setContentText(yourService.getString(R.string.content));
    
        Notification notification = builder.build();
    
        yourService.startForeground(notificationId, notification);
    
        if (notificationId != lastShownNotificationId) {
              // Cancel previous notification
              final NotificationManager nm = (NotificationManager) yourService.getSystemService(Activity.NOTIFICATION_SERVICE);
              nm.cancel(lastShownNotificationId);
        }
        lastShownNotificationId = notificationId;
    }
    
    public static NotificationCompat.Builder getNotificationBuilder(Context context, String channelId, int importance) {
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            prepareChannel(context, channelId, importance);
            builder = new NotificationCompat.Builder(context, channelId);
        } else {
            builder = new NotificationCompat.Builder(context);
        }
        return builder;
    }
    
    @TargetApi(26)
    private static void prepareChannel(Context context, String id, int importance) {
        final String appName = context.getString(R.string.app_name);
        String description = context.getString(R.string.notifications_channel_description);
        final NotificationManager nm = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
    
        if(nm != null) {
            NotificationChannel nChannel = nm.getNotificationChannel(id);
    
            if (nChannel == null) {
                nChannel = new NotificationChannel(id, appName, importance);
                nChannel.setDescription(description);
                nm.createNotificationChannel(nChannel);
            }
        }
    }
    

    Remember that your foreground notification will have the same state as your other notifications even if you'll use different channel ids, so it might be hidden as a group with others. Use different groups to avoid it.

    0 讨论(0)
  • 2020-12-01 11:27

    In my case, it was caused by me using IntentService.

    In short, if you want a foreground service then subclass Service.

    0 讨论(0)
  • 2020-12-01 11:36

    The problem was i am using Android O and it requires more information. Here is the successful code for android O.

        mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager);
        mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor
                (ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE);
        mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build());
    
    @TargetApi(26)
    private void createChannel(NotificationManager notificationManager) {
        String name = "FileDownload";
        String description = "Notifications for download status";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
    
        NotificationChannel mChannel = new NotificationChannel(name, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.BLUE);
        notificationManager.createNotificationChannel(mChannel);
    }
    
    0 讨论(0)
提交回复
热议问题