Skip to content

Commit

Permalink
Merge pull request #73 from sagium/android_api_26_27_compatibilty
Browse files Browse the repository at this point in the history
Fix ANRs when running on android 8.0 or higher.
  • Loading branch information
vikeri authored Mar 23, 2018
2 parents 5fc0f02 + dd20672 commit c09ffb2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ buildscript {
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
compileSdkVersion 27
buildToolsVersion '25.0.3'
// Uncomment to develop with yarn link into node_modules
// compileOptions.incremental = false
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 27
versionCode 2
versionName "2.0"
ndk {
Expand All @@ -37,7 +37,7 @@ repositories {
}

dependencies {
compile 'com.firebase:firebase-jobdispatcher:0.7.0'
compile 'com.firebase:firebase-jobdispatcher:0.8.5'
compile 'com.facebook.react:react-native:+'
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

class ReactNativeEventStarter {
private static final String LOG_TAG = ReactNativeEventStarter.class.getSimpleName();
Expand All @@ -35,6 +40,29 @@ public void trigger(@NonNull Bundle jobBundle) {
public static class MyHeadlessJsTaskService extends HeadlessJsTaskService {
private static final String LOG_TAG = MyHeadlessJsTaskService.class.getSimpleName();

@Override
@SuppressLint("WrongConstant")
public void onCreate() {
super.onCreate();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Context mContext = this.getApplicationContext();
String CHANNEL_ID = "Background job";

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

Notification notification =
new Notification.Builder(mContext, CHANNEL_ID)
.setContentTitle("Running background job")
.setContentText(mContext.getPackageName())
.setSmallIcon(R.drawable.ic_notification)
.build();

startForeground(1, notification);
}
}

@Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Log.d(LOG_TAG, "getTaskConfig() called with: intent = [" + intent + "]");
Bundle extras = intent.getExtras();
Expand All @@ -56,7 +84,12 @@ public static void start(Context context, Bundle jobBundle) {
"start() called with: context = [" + context + "], jobBundle = [" + jobBundle + "]");
Intent starter = new Intent(context, MyHeadlessJsTaskService.class);
starter.putExtras(jobBundle);
context.startService(starter);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(starter);
}else{
context.startService(starter);
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c09ffb2

Please sign in to comment.