Fix crash(es) due to uninitialized notification channel. (#1906)

* Fix crash(es) due to uninitialized notification channel.

The notification channel needs to be created for API versions greater than
OR EQUAL to 26 (O).  Also, the channel does not need to be reinitialized
if it already exists.

* Initialize notification channel when Service is created.
This commit is contained in:
Dmitry Brant 2018-09-27 11:39:32 -04:00 committed by Josephine Lim
parent 1b7c72e192
commit e0a79f89e9
2 changed files with 13 additions and 12 deletions

View file

@ -8,6 +8,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import com.facebook.drawee.backends.pipeline.Fresco;
@ -122,22 +123,22 @@ public class CommonsApplication extends Application {
Stetho.initializeWithDefaults(this);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
createNotificationChannel();
}
createNotificationChannel(this);
// Fire progress callbacks for every 3% of uploaded content
System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0");
}
@RequiresApi(26)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID_ALL,
getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_NONE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
public static void createNotificationChannel(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(NOTIFICATION_CHANNEL_ID_ALL);
if (channel == null) {
channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_ALL,
context.getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
}
}
/**