Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to AndroidX & latest SDK #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -40,6 +39,6 @@ android {
disable 'InvalidPackage'
}
dependencies {
compile "com.android.support:support-core-utils:25.3.1"
implementation "androidx.appcompat:appcompat:$supportLibVersion"
}
}
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.os.Build;
import android.graphics.Color;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import androidx.core.app.NotificationCompat;

public class ForegroundService extends Service {
private static final String TAG = "ForegroundService";
public static int ONGOING_NOTIFICATION_ID = 1;

@Override
public void onCreate() {
Expand All @@ -28,7 +31,44 @@ public int onStartCommand(Intent intent, int flags, int startId) {

Bundle bundle = intent.getExtras();

Notification notification =

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
int ONGOING_NOTIFICATION_ID = 2;

String NOTIFICATION_CHANNEL_ID = "com.pauldemarco.foregroundservice";
String channelName = "Foreground Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
channelName, NotificationManager.IMPORTANCE_NONE);

chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(
getApplicationContext().NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

Notification notification = notificationBuilder
.setOngoing(true)
// .setSmallIcon(R.drawable.icon_1)
.setContentTitle(bundle.getString("title"))
.setContentText(bundle.getString("text"))
.setSubText(bundle.getString("subText"))
.setTicker(bundle.getString("ticker"))
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();

startForeground(ONGOING_NOTIFICATION_ID, notification);
}
else
{
int ONGOING_NOTIFICATION_ID = 1;

Notification notification =
new NotificationCompat.Builder(this)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
Expand All @@ -39,8 +79,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentIntent(pendingIntent)
.build();

startForeground(ONGOING_NOTIFICATION_ID, notification);

startForeground(ONGOING_NOTIFICATION_ID, notification);
}

} else if (ForegroundServicePlugin.STOPFOREGROUND_ACTION.equals(intent.getAction())) {
stopForeground(true);
Expand Down
13 changes: 7 additions & 6 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28

lintOptions {
disable 'InvalidPackage'
Expand All @@ -26,7 +25,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.pauldemarco.foregroundserviceexample"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -46,7 +45,9 @@ flutter {
}

dependencies {
androidTestCompile 'com.android.support:support-annotations:25.4.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
testImplementation 'junit:junit:4.12'
def supportLibVersion = rootProject.ext.supportLibVersion
androidTestImplementation 'androidx.test:runner:1.1.1' // or higher
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' // or higher
implementation "androidx.appcompat:appcompat:$supportLibVersion"
}
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
<service android:name="com.pauldemarco.foregroundservice.ForegroundService" >
</service>
</application>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
</manifest>
8 changes: 7 additions & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.2.1'
}

ext {
compileSdkVersion = 28 // or higher
targetSdkVersion = 28 // or higher
supportLibVersion = "1.0.2" // or higher
}
}

Expand Down
2 changes: 2 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: foreground_service
description: Android's ForegroundService for Flutter
description: Android ForegroundService for Flutter
version: 0.0.1
author:
homepage:

environment:
sdk: ">=2.1.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
Expand Down