Skip to content

Commit

Permalink
Make sure foreground LndMobileService follows Android 14 policies
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjoberg authored Oct 4, 2023
1 parent 8da942a commit c1a2391
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
10 changes: 9 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

<application
android:name=".MainApplication"
Expand Down Expand Up @@ -107,6 +108,13 @@

<service
android:name=".LndMobileService"
android:exported="false" />
android:exported="false"
android:foregroundServiceType="specialUse"
>
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Run Bitcoin Lightning Network node lnd in order for the user to receive incoming payments"
/>
</service>
</application>
</manifest>
30 changes: 21 additions & 9 deletions android/app/src/main/java/com/blixtwallet/LndMobileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.RemoteException;
import android.util.Base64;
import android.util.Log;
import static android.app.Notification.FOREGROUND_SERVICE_IMMEDIATE;

import lndmobile.Callback;
import lndmobile.Lndmobile;
Expand Down Expand Up @@ -556,6 +557,7 @@ public int onStartCommand(Intent intent, int flags, int startid) {
boolean persistentServicesEnabled = getPersistentServicesEnabled(this);
// persistent services on, start service as foreground-svc
if (persistentServicesEnabled) {
Notification.Builder notificationBuilder = null;
Intent notificationIntent = new Intent (this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
Expand All @@ -565,19 +567,29 @@ public int onStartCommand(Intent intent, int flags, int startid) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(chan);

notificationBuilder = new Notification.Builder(this, BuildConfig.APPLICATION_ID);
} else {
notificationBuilder = new Notification.Builder(this);
}

notificationBuilder
.setContentTitle("LND")
.setContentText("LND is running in the background")
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentIntent(pendingIntent)
.setTicker("Blixt Wallet")
.setOngoing(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
notificationBuilder.setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE);
}
Notification notification = new Notification.Builder(this, BuildConfig.APPLICATION_ID)
.setContentTitle("LND")
.setContentText("LND is running in the background")
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentIntent(pendingIntent)
.setTicker("Blixt Wallet")
.setOngoing(true)
.build();

Notification notification = notificationBuilder.build();
startForeground(ONGOING_NOTIFICATION_ID, notification);
}
}

// else noop, instead of calling startService, start will be handled by binding
return startid;
}
Expand Down
6 changes: 3 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

buildscript {
ext {
buildToolsVersion = "33.0.0"
buildToolsVersion = "34.0.0"
minSdkVersion = 23
compileSdkVersion = 33
targetSdkVersion = 33
compileSdkVersion = 34
targetSdkVersion = 34

// We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
ndkVersion = "23.1.7779620"
Expand Down

1 comment on commit c1a2391

@vercel
Copy link

@vercel vercel bot commented on c1a2391 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blixt-wallet – ./

blixt-wallet-hsjoberg.vercel.app
blixt-wallet-git-master-hsjoberg.vercel.app

Please sign in to comment.