Create notification channel on Application subclass

Julien LepillerWed Jun 10 15:13:33+0200 2020

b6598b5

Create notification channel on Application subclass

app/src/main/AndroidManifest.xml

66
    <uses-permission android:name="android.permission.INTERNET" />
77
88
    <application
9+
        android:name=".App"
910
        android:allowBackup="true"
1011
        android:icon="@mipmap/ic_launcher"
1112
        android:label="@string/app_name"

app/src/main/java/eu/lepiller/nani/App.java unknown status 1

1+
package eu.lepiller.nani;
2+
3+
import android.app.Application;
4+
import android.app.NotificationChannel;
5+
import android.app.NotificationManager;
6+
7+
public class App extends Application {
8+
    public static final String DICTIONARY_DOWNLOAD_NOTIFICATION_CHANNEL = "dictionaryDownloadChannel";
9+
10+
    @Override
11+
    public void onCreate() {
12+
        super.onCreate();
13+
14+
        createNotificationChannel();
15+
    }
16+
17+
    private void createNotificationChannel() {
18+
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
19+
            NotificationChannel channel = new NotificationChannel(DICTIONARY_DOWNLOAD_NOTIFICATION_CHANNEL,
20+
                    "Nani's dictionary download notification", NotificationManager.IMPORTANCE_DEFAULT);
21+
22+
            NotificationManager manager =
23+
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
24+
            if(manager != null)
25+
                manager.createNotificationChannel(channel);
26+
        }
27+
    }
28+
}

app/src/main/java/eu/lepiller/nani/MainActivity.java

5555
5656
        Button radical_button = findViewById(R.id.radical_button);
5757
58-
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
59-
            NotificationChannel channel = new NotificationChannel("dico_dll",
60-
                    "Nani's dictionary download notification", NotificationManager.IMPORTANCE_DEFAULT);
61-
            NotificationManager manager =
62-
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
63-
            if(manager != null)
64-
                manager.createNotificationChannel(channel);
65-
        }
66-
6758
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
6859
6960
        SharedPreferences sharedPref =