13

これまでのところ、ContextCompat.startForegroundService(context, intentService);を使用してサービスを開始するようにコードをアシストしました。この方法では、アンドロイド< 26とアンドロイド26(オレオ)でも動作します。Oreo - フォアグラウンドサービスでフォアグラウンド通知が表示されない

私はまだ違いが見えます。アンドロイド・オレオ私のカスタム・フォアグラウンド通知は表示されません(「アプリはバックグラウンドで実行中」通知のみ表示されます)。私もそこに何かを調整する必要がありますか?

は私のサービスは、次のようになります。

public class BaseOverlayService extends Service { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     moveToForeground(); 
    } 

    private void moveToForeground() { 
     Notification notification = ...; 
     super.startForeground(NOTIFICATION_ID, notification); 
    } 
} 

公式例

この例(https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200が)私は、次の使用する必要があることを、コメントとして示しているが、startServiceInForeground存在しません...

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { 
    NotificationManager mNotificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); 
    mNotificationManager.startServiceInForeground(new Intent(this, BaseOverlayService.class), NOTIFICATION_ID, notification); 
} else { 
    startForeground(NOTIFICATION_ID, notification); 
} 

私の通知は、今までのAPI < 26とAndroidデバイスの数千人に取り組んでおり、このように作成されます。

protected Notification foregroundNotification(int notificationId) 
{ 
    boolean paused = MainApp.getPrefs().sidebarServicePaused(); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setSmallIcon(R.drawable.icon_not); 
    builder.setContentIntent(notificationIntent()); 
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon)); 
    builder.setContentTitle(getString(R.string.derived_app_name)); 
    builder.setContentText(getString(checkStatus() ? (paused ? R.string.service_notification_text_paused : R.string.service_notification_text_running) : R.string.service_notification_text_preparing)); 
    builder.setColor(Color.parseColor("#25baa2")); 

    if (MainApp.getPrefs().hideNotificationIcon()) 
     builder.setPriority(NotificationCompat.PRIORITY_MIN); 
    else 
     builder.setPriority(NotificationCompat.PRIORITY_MAX); 

    if (paused) 
     builder.addAction(R.drawable.ic_play_arrow_black_24dp, getString(R.string.resume), resumeIntent(this)); 
    else 
     builder.addAction(R.drawable.ic_pause_black_24dp, getString(R.string.pause), pauseIntent(this)); 

    return builder.build(); 
} 

答えて

11

あなたはアプリの通知チャネルを定義する必要があるかもしれません。ログを確認すると、警告が表示されます。 紹介についてthisを確認してください。

私はあなたにいくつかの例を教えてください、それはkotinになります。 まず、このようなクラスを作成します。アプリケーションの開始時にcreateMainNotificationChannel()を1回呼び出す必要があります。

class NotificationManager(private val context: Context) { 

    companion object { 
     private val CHANNEL_ID = "YOUR_CHANNEL_ID" 
     private val CHANNEL_NAME = "Your human readable notification channel name" 
     private val CHANNEL_DESCRIPTION = "description" 
    } 

    @RequiresApi(Build.VERSION_CODES.O) 
    fun getMainNotificationId(): String { 
     return CHANNEL_ID 
    } 

    @RequiresApi(Build.VERSION_CODES.O) 
    fun createMainNotificationChannel() { 
      val id = CHANNEL_ID 
      val name = CHANNEL_NAME 
      val description = CHANNEL_DESCRIPTION 
      val importance = android.app.NotificationManager.IMPORTANCE_LOW 
      val mChannel = NotificationChannel(id, name, importance) 
      mChannel.description = description 
      mChannel.enableLights(true) 
      mChannel.lightColor = Color.RED 
      mChannel.enableVibration(true) 
      val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager 
      mNotificationManager.createNotificationChannel(mChannel) 
    } 
} 

は、その後、あなたがutilのこの

fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId) 
    } else { 
     return NotificationCompat.Builder(context) 
    } 
} 
+0

のように使用することができ、追加したリンクは、おかげでとにかくかなり良いです。しかし、1つの質問:これは、私は必ずしもフォアグラウンドサービスの通知をアンドロイドオレオで表示する必要はありません、この変更は意味ですか?人々は私のアプリが通知なしでアンドロイド・オレオに取り組んでいると私に言いました... – prom85

+1

eghmmm ...実際には通知なしで動作することはできません。背景や仕事は、活動や通知などの目に見える構成要素がないとアンドロイドで許可されません。可視コンポーネントシステムなしでサービスを実行すると、しばらくしてからサービスが終了します。バックグラウンドジョブをしたい場合は、サービスの通知が必要です。 期間を置いて何かをバックグラウンドで実行する必要がある場合は、[firebase job dispatcher](https://github.com/firebase/firebase-jobdispatcher-android)をチェックする必要があります。スケジュールされたサービスを実行できます。 –

+0

私は 'WindowManager'にUI要素を持つサイドバーアプリケーションを持っています。これは今まで私が使用していた目に見えない通知(見えない通知はチャネルなしのもの)とうまくいくようです。数日後には何の不満もありませんでした。エミュレータは5秒以内にサービスを終了しません – prom85

関連する問題