Android O Notification Building Fix (#655)

-Added simple notification channel for Android O.
-Fixes notification building failure for background and popup player on Android O.
-Reduce notification channel importance to low to avoid making noise on every notification update.
This commit is contained in:
Tonelico 2017-08-18 05:05:31 -07:00 committed by Mauricio Colli
parent 9fbac943bb
commit 85108be686
4 changed files with 34 additions and 4 deletions

View File

@ -1,7 +1,10 @@
package org.schabi.newpipe;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import com.facebook.stetho.Stetho;
import com.nostra13.universalimageloader.core.ImageLoader;
@ -89,6 +92,8 @@ public class App extends Application {
SettingsActivity.initSettings(this);
ThemeHelper.setTheme(getApplicationContext());
initNotificationChannel();
}
/**
@ -112,4 +117,23 @@ public class App extends Application {
public static boolean isUsingTor() {
return useTor;
}
public void initNotificationChannel() {
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
return;
}
final String id = getString(R.string.notification_channel_id);
final CharSequence name = getString(R.string.notification_channel_name);
final String description = getString(R.string.notification_channel_description);
// Keep this below DEFAULT to avoid making noise on every notification update
final int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
}
}

View File

@ -145,13 +145,13 @@ public class BackgroundPlayer extends Service {
setupNotification(notRemoteView);
setupNotification(bigNotRemoteView);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setOngoing(true)
.setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCustomContentView(notRemoteView)
.setCustomBigContentView(bigNotRemoteView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) builder.setPriority(Notification.PRIORITY_MAX);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) builder.setPriority(NotificationCompat.PRIORITY_MAX);
return builder;
}

View File

@ -168,9 +168,11 @@ public class PopupVideoPlayer extends Service {
float defaultSize = getResources().getDimension(R.dimen.popup_default_width);
popupWidth = popupRememberSizeAndPos ? sharedPreferences.getFloat(POPUP_SAVED_WIDTH, defaultSize) : defaultSize;
final int layoutParamType = Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_PHONE : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
windowLayoutParams = new WindowManager.LayoutParams(
(int) popupWidth, (int) getMinimumVideoHeight(popupWidth),
WindowManager.LayoutParams.TYPE_PHONE,
layoutParamType,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
windowLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
@ -225,7 +227,7 @@ public class PopupVideoPlayer extends Service {
break;
}
return new NotificationCompat.Builder(this)
return new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setOngoing(true)
.setSmallIcon(R.drawable.ic_play_arrow_white)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)

View File

@ -117,6 +117,10 @@
<string name="popup_resizing_indicator_title">Resizing</string>
<string name="best_resolution">Best resolution</string>
<string name="notification_channel_id" translatable="false">newpipe</string>
<string name="notification_channel_name">NewPipe Notification</string>
<string name="notification_channel_description">Notifications For NewPipe Background and Popup Players</string>
<!-- error strings -->
<string name="general_error">Error</string>
<string name="network_error">Network error</string>