Skip to content

Commit 07de70b

Browse files
Fix notifications
1 parent 9abc000 commit 07de70b

File tree

5 files changed

+91
-131
lines changed

5 files changed

+91
-131
lines changed

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ android {
5252
applicationId "io.stormbird.wallet"
5353
minSdk 24
5454
targetSdk 34
55-
versionCode 263
55+
versionCode 266
5656
versionName "3.81"
5757

5858
android.buildFeatures.buildConfig true
@@ -64,13 +64,13 @@ android {
6464
def DEFAULT_INFURA_API_KEY = "\"da3717f25f824cc1baa32d812386d93f\""
6565
def DEFAULT_OPENSEA_API_KEY = "\"...\""; //Put your OpenSea developer API key in here, otherwise you are reliant on the backup NFT fetch method (which usually works ok)
6666
def DEFAULT_POLYGONSCAN_API_KEY = "\"...\""; //Put your Polygonscan developer API key in here to get access to Polygon/Mumbai token discovery and transactions
67-
def DEFUALT_WALLETCONNECT_PROJECT_ID = "\"40c6071febfd93f4fe485c232a8a4cd9\""
67+
def DEFAULT_WALLETCONNECT_PROJECT_ID = "\"40c6071febfd93f4fe485c232a8a4cd9\""
6868
def DEFAULT_AURORA_API_KEY = "\"HFDDY5BNKGXBB82DE2G8S64C3C41B76PYI\""; //Put your Aurorascan.dev API key here - this one will rate limit as it is common
6969

7070
buildConfigField 'int', 'DB_VERSION', '54'
7171

7272
buildConfigField "String", XInfuraAPI, DEFAULT_INFURA_API_KEY
73-
buildConfigField "String", "WALLETCONNECT_PROJECT_ID", DEFUALT_WALLETCONNECT_PROJECT_ID
73+
buildConfigField "String", "WALLETCONNECT_PROJECT_ID", DEFAULT_WALLETCONNECT_PROJECT_ID
7474

7575
buildConfigField "String", "NOTIFICATION_API_BASE_URL", NOTIFICATION_API_BASE_URL
7676

@@ -92,7 +92,7 @@ android {
9292
cFlags "-DOSKEY=" + DEFAULT_OPENSEA_API_KEY + ""
9393
cFlags "-DPSKEY=" + DEFAULT_POLYGONSCAN_API_KEY + ""
9494
cFlags "-DASKEY=" + DEFAULT_AURORA_API_KEY + ""
95-
cFlags "-DWCKEY=" + DEFUALT_WALLETCONNECT_PROJECT_ID + ""
95+
cFlags "-DWCKEY=" + DEFAULT_WALLETCONNECT_PROJECT_ID + ""
9696
}
9797
cmake {
9898
cFlags "-Wno-dev"

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
1414
<!-- Note: this permission is used to display the active WalletConnect Notification. This is a courtesy to the user to inform them a WalletConnect channel is active -->
1515
<!-- This is pegged to class WalletConnectV2Service which has no data connection, the data connection is only active when the AlphaWallet app is open -->
16-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
16+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
1717
<uses-permission android:name="com.google.android.gms.permission.AD_ID" tools:node="remove"/>
1818

1919
<uses-feature
@@ -121,14 +121,6 @@
121121
android:name=".ui.SplashActivity"
122122
android:theme="@style/AppTheme.NoActionBar.Splash" />
123123

124-
<!-- Note: this service is used to display the active WalletConnect Notification. This is a courtesy to the user to inform them a WalletConnect channel is active -->
125-
<service
126-
android:name=".service.WalletConnectV2Service"
127-
android:enabled="true"
128-
android:foregroundServiceType="specialUse"
129-
android:exported="false">
130-
</service>
131-
132124
<service
133125
android:name=".service.AlphaWalletFirebaseMessagingService"
134126
android:exported="false">

app/src/main/java/com/alphawallet/app/service/WalletConnectV2Service.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

app/src/main/java/com/alphawallet/app/ui/HomeActivity.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -931,27 +931,22 @@ private void hideDialog()
931931
}
932932
}
933933

934-
private boolean checkNotificationPermission(int permissionTag)
934+
private void checkNotificationPermission(int permissionTag)
935935
{
936-
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
937-
!= PackageManager.PERMISSION_GRANTED)
936+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
937+
!= PackageManager.PERMISSION_GRANTED)
938+
{
939+
String[] permissions;
940+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU)
938941
{
939-
String[] permissions;
940-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU)
941-
{
942-
permissions = new String[]{Manifest.permission.POST_NOTIFICATIONS};
943-
}
944-
else
945-
{
946-
permissions = new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY};
947-
}
948-
requestPermissions(permissions, permissionTag);
949-
return false;
942+
permissions = new String[]{Manifest.permission.POST_NOTIFICATIONS};
950943
}
951944
else
952945
{
953-
return true;
946+
permissions = new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY};
954947
}
948+
requestPermissions(permissions, permissionTag);
949+
}
955950
}
956951

957952
@Override

app/src/main/java/com/alphawallet/app/walletconnect/AWWalletConnectClient.java

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
import static com.walletconnect.web3.wallet.client.Wallet.Model;
66
import static com.walletconnect.web3.wallet.client.Wallet.Params;
77

8+
import android.Manifest;
89
import android.app.Activity;
910
import android.app.Application;
11+
import android.app.Notification;
12+
import android.app.NotificationChannel;
13+
import android.app.NotificationManager;
14+
import android.app.PendingIntent;
1015
import android.content.Context;
1116
import android.content.Intent;
12-
import android.content.pm.ServiceInfo;
17+
import android.content.pm.PackageManager;
1318
import android.os.Build;
1419
import android.os.Handler;
1520
import android.os.Looper;
@@ -18,8 +23,12 @@
1823

1924
import androidx.activity.result.ActivityResultLauncher;
2025
import androidx.annotation.NonNull;
21-
import androidx.core.content.ContextCompat;
26+
import androidx.annotation.RequiresApi;
27+
import androidx.core.app.ActivityCompat;
28+
import androidx.core.app.NotificationCompat;
29+
import androidx.core.app.NotificationManagerCompat;
2230
import androidx.lifecycle.MutableLiveData;
31+
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2332

2433
import com.alphawallet.app.App;
2534
import com.alphawallet.app.C;
@@ -34,7 +43,7 @@
3443
import com.alphawallet.app.repository.KeyProviderFactory;
3544
import com.alphawallet.app.repository.PreferenceRepositoryType;
3645
import com.alphawallet.app.service.GasService;
37-
import com.alphawallet.app.service.WalletConnectV2Service;
46+
import com.alphawallet.app.ui.WalletConnectNotificationActivity;
3847
import com.alphawallet.app.ui.WalletConnectSessionActivity;
3948
import com.alphawallet.app.ui.WalletConnectV2Activity;
4049
import com.alphawallet.app.ui.widget.entity.ActionSheetCallback;
@@ -86,6 +95,7 @@ public class AWWalletConnectClient implements Web3Wallet.WalletDelegate
8695
private boolean hasConnection;
8796
private Application application;
8897
private final PreferenceRepositoryType preferenceRepository;
98+
private final int WC_NOTIFICATION_ID = 25964950;
8999

90100
public AWWalletConnectClient(Context context, WalletConnectInteract walletConnectInteract, PreferenceRepositoryType preferenceRepository, GasService gasService)
91101
{
@@ -270,18 +280,13 @@ private void updateService(List<WalletConnectSessionItem> items)
270280
{
271281
try
272282
{
273-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
283+
if (items.isEmpty())
274284
{
275-
if (items.isEmpty())
276-
{
277-
context.stopService(new Intent(context, WalletConnectV2Service.class));
278-
//now signal
279-
}
280-
else
281-
{
282-
Intent serviceIntent = new Intent(context, WalletConnectV2Service.class);
283-
ContextCompat.startForegroundService(context, serviceIntent);
284-
}
285+
removeNotification();
286+
}
287+
else
288+
{
289+
displayNotification();
285290
}
286291
}
287292
catch (Exception e)
@@ -291,6 +296,63 @@ private void updateService(List<WalletConnectSessionItem> items)
291296
}
292297
}
293298

299+
public void displayNotification()
300+
{
301+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
302+
{
303+
createNotificationChannel();
304+
}
305+
Notification notification = createNotification();
306+
307+
// Issue the notification if we have user permission
308+
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
309+
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED)
310+
{
311+
notificationManager.notify(WC_NOTIFICATION_ID, notification);
312+
}
313+
else
314+
{
315+
Intent intent = new Intent(C.REQUEST_NOTIFICATION_ACCESS);
316+
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
317+
}
318+
}
319+
320+
final String CHANNEL_ID = "WalletConnectV2Service";
321+
private Notification createNotification()
322+
{
323+
Intent notificationIntent = new Intent(context, WalletConnectNotificationActivity.class);
324+
PendingIntent pendingIntent = PendingIntent.getActivity(context, WC_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
325+
326+
return new NotificationCompat.Builder(context, CHANNEL_ID)
327+
.setContentTitle(context.getString(R.string.notify_wallet_connect_title))
328+
.setContentText(context.getString(R.string.notify_wallet_connect_content))
329+
.setSmallIcon(R.drawable.ic_logo)
330+
.setContentIntent(pendingIntent)
331+
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
332+
.build();
333+
}
334+
335+
@RequiresApi(api = Build.VERSION_CODES.O)
336+
private void createNotificationChannel()
337+
{
338+
CharSequence name = context.getString(R.string.notify_wallet_connect_title);
339+
String description = context.getString(R.string.notify_wallet_connect_content);
340+
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
341+
channel.setDescription(description);
342+
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
343+
notificationManager.createNotificationChannel(channel);
344+
}
345+
346+
//remove notification
347+
private void removeNotification()
348+
{
349+
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
350+
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED)
351+
{
352+
notificationManager.cancel(WC_NOTIFICATION_ID);
353+
}
354+
}
355+
294356

295357
public void reject(Model.SessionProposal sessionProposal, WalletConnectV2Callback callback)
296358
{

0 commit comments

Comments
 (0)