support notification channels from Android O

This commit is contained in:
Boris Timofeev 2017-09-01 18:12:07 +03:00
parent 70db06eebc
commit 3f56868998
4 changed files with 26 additions and 3 deletions

View file

@ -21,9 +21,20 @@
package org.emunix.unipatcher;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
public class UniPatcher extends Application {
private static String appArgument = null;
public static final String NOTIFICATION_CHANNEL_ID = "notifications";
@Override
public void onCreate() {
super.onCreate();
initNotificationChannel();
}
public static String getAppArgument() {
return appArgument;
@ -32,4 +43,15 @@ public class UniPatcher extends Application {
public static void setAppArgument(String appArgument) {
UniPatcher.appArgument = appArgument;
}
public void initNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
}

View file

@ -326,7 +326,7 @@ public class WorkerService extends IntentService {
private void showErrorNotification(String text) {
Intent notificationIntent = new Intent(this, MainActivity.class);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notify = new NotificationCompat.Builder(this)
Notification notify = new NotificationCompat.Builder(this, UniPatcher.NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.notify_error))
.setContentText(text)
.setSmallIcon(R.drawable.ic_stat_patching)

View file

@ -24,6 +24,7 @@ import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import org.emunix.unipatcher.UniPatcher;
import org.emunix.unipatcher.ui.activity.MainActivity;
public abstract class Notify {
@ -40,9 +41,8 @@ public abstract class Notify {
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyBuilder = new NotificationCompat.Builder(context);
notifyBuilder = new NotificationCompat.Builder(context, UniPatcher.NOTIFICATION_CHANNEL_ID);
notifyBuilder.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));
//notifyMng = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notifyMng = NotificationManagerCompat.from(context);
}

View file

@ -167,6 +167,7 @@
<string name="donation">Donation</string>
<string name="icon">icon</string>
<string name="notification_channel_name">Notifications</string>
</resources>