use NotificationChannel to work on O and beyond with SDK 26

Notifications don't work on Oreo without this change, which includes a
new Support Library in order to get NotificationChannel and creates and
uses that as docs describe. Requires that MinSDK be raised from 8 to 14,
which may lock some users out. It *should* be possible not to do this in
the fdroid variant since their app store doesn't requires SDK 26, but
I'll look at that later.
This commit is contained in:
Eric House 2018-09-13 19:49:49 -07:00
parent 6f6c3238d4
commit 45600a295a
2 changed files with 36 additions and 3 deletions

View file

@ -32,7 +32,7 @@ android {
// default changes and .travis.yml can be kept in sync
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 8
minSdkVersion 14
targetSdkVersion 26
versionCode VERSION_CODE_BASE
versionName VERSION_NAME
@ -197,11 +197,16 @@ android {
}
}
ext {
SUPPORT_LIB_VERSION = '27.1.1'
}
dependencies {
// Look into replacing this with a fetch too PENDING
xw4Implementation files('../libs/gcm.jar')
implementation 'com.android.support:support-v4:23.4.0'
implementation "com.android.support:support-v4:$SUPPORT_LIB_VERSION"
implementation "com.android.support:support-compat:$SUPPORT_LIB_VERSION"
// 2.6.8 is probably as far forward as I can go without upping my
// min-supported SDK version

View file

@ -23,6 +23,7 @@ package org.eehouse.android.xw4;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
@ -38,6 +39,7 @@ import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract.PhoneLookup;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;
@ -81,6 +83,8 @@ public class Utils {
private static final String TAG = Utils.class.getSimpleName();
public static final int TURN_COLOR = 0x7F00FF00;
private static final String CHANNEL_ID = BuildConfig.APPLICATION_ID + "_channel_id";
private static final String DB_PATH = "XW_GAMES";
private static final String HIDDEN_PREFS = "xwprefs_hidden";
private static final String FIRST_VERSION_KEY = "FIRST_VERSION_KEY";
@ -236,6 +240,8 @@ public class Utils {
String title, String body,
int id )
{
makeNotificationChannel( context );
/* nextRandomInt: per this link
http://stackoverflow.com/questions/10561419/scheduling-more-than-one-pendingintent-to-same-activity-using-alarmmanager
one way to avoid getting the same PendingIntent for similar
@ -254,9 +260,10 @@ public class Utils {
defaults |= Notification.DEFAULT_VIBRATE;
}
Notification notification = new NotificationCompat.Builder( context )
Notification notification = new NotificationCompat.Builder( context, CHANNEL_ID )
.setContentIntent( pi )
.setSmallIcon( R.drawable.notify )
.setPriority(NotificationCompat.PRIORITY_LOW)
//.setTicker(body)
//.setWhen(time)
.setAutoCancel( true )
@ -611,4 +618,25 @@ public class Utils {
}
}
private static boolean sChannelMade = false;
private static void makeNotificationChannel( Context context )
{
if ( !sChannelMade ) {
sChannelMade = true;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
NotificationManager notMgr = (NotificationManager)
context.getSystemService( Context.NOTIFICATION_SERVICE );
String channelDescription = "XWORDS Default Channel";
NotificationChannel channel = notMgr.getNotificationChannel( CHANNEL_ID );
if ( channel == null ) {
channel = new NotificationChannel( CHANNEL_ID, channelDescription,
NotificationManager.IMPORTANCE_LOW );
// channel.setLightColor(Color.GREEN);
channel.enableVibration( true );
notMgr.createNotificationChannel( channel );
}
}
}
}
}