Skip to content

Commit

Permalink
Fix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Apr 23, 2024
1 parent 9abc000 commit 07de70b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 131 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ android {
applicationId "io.stormbird.wallet"
minSdk 24
targetSdk 34
versionCode 263
versionCode 266
versionName "3.81"

android.buildFeatures.buildConfig true
Expand All @@ -64,13 +64,13 @@ android {
def DEFAULT_INFURA_API_KEY = "\"da3717f25f824cc1baa32d812386d93f\""
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)
def DEFAULT_POLYGONSCAN_API_KEY = "\"...\""; //Put your Polygonscan developer API key in here to get access to Polygon/Mumbai token discovery and transactions
def DEFUALT_WALLETCONNECT_PROJECT_ID = "\"40c6071febfd93f4fe485c232a8a4cd9\""
def DEFAULT_WALLETCONNECT_PROJECT_ID = "\"40c6071febfd93f4fe485c232a8a4cd9\""
def DEFAULT_AURORA_API_KEY = "\"HFDDY5BNKGXBB82DE2G8S64C3C41B76PYI\""; //Put your Aurorascan.dev API key here - this one will rate limit as it is common

buildConfigField 'int', 'DB_VERSION', '54'

buildConfigField "String", XInfuraAPI, DEFAULT_INFURA_API_KEY
buildConfigField "String", "WALLETCONNECT_PROJECT_ID", DEFUALT_WALLETCONNECT_PROJECT_ID
buildConfigField "String", "WALLETCONNECT_PROJECT_ID", DEFAULT_WALLETCONNECT_PROJECT_ID

buildConfigField "String", "NOTIFICATION_API_BASE_URL", NOTIFICATION_API_BASE_URL

Expand All @@ -92,7 +92,7 @@ android {
cFlags "-DOSKEY=" + DEFAULT_OPENSEA_API_KEY + ""
cFlags "-DPSKEY=" + DEFAULT_POLYGONSCAN_API_KEY + ""
cFlags "-DASKEY=" + DEFAULT_AURORA_API_KEY + ""
cFlags "-DWCKEY=" + DEFUALT_WALLETCONNECT_PROJECT_ID + ""
cFlags "-DWCKEY=" + DEFAULT_WALLETCONNECT_PROJECT_ID + ""
}
cmake {
cFlags "-Wno-dev"
Expand Down
10 changes: 1 addition & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<!-- 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 -->
<!-- This is pegged to class WalletConnectV2Service which has no data connection, the data connection is only active when the AlphaWallet app is open -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="com.google.android.gms.permission.AD_ID" tools:node="remove"/>

<uses-feature
Expand Down Expand Up @@ -121,14 +121,6 @@
android:name=".ui.SplashActivity"
android:theme="@style/AppTheme.NoActionBar.Splash" />

<!-- 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 -->
<service
android:name=".service.WalletConnectV2Service"
android:enabled="true"
android:foregroundServiceType="specialUse"
android:exported="false">
</service>

<service
android:name=".service.AlphaWalletFirebaseMessagingService"
android:exported="false">
Expand Down

This file was deleted.

25 changes: 10 additions & 15 deletions app/src/main/java/com/alphawallet/app/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -931,27 +931,22 @@ private void hideDialog()
}
}

private boolean checkNotificationPermission(int permissionTag)
private void checkNotificationPermission(int permissionTag)
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED)
{
String[] permissions;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU)
{
String[] permissions;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU)
{
permissions = new String[]{Manifest.permission.POST_NOTIFICATIONS};
}
else
{
permissions = new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY};
}
requestPermissions(permissions, permissionTag);
return false;
permissions = new String[]{Manifest.permission.POST_NOTIFICATIONS};
}
else
{
return true;
permissions = new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY};
}
requestPermissions(permissions, permissionTag);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import static com.walletconnect.web3.wallet.client.Wallet.Model;
import static com.walletconnect.web3.wallet.client.Wallet.Params;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
Expand All @@ -18,8 +23,12 @@

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.alphawallet.app.App;
import com.alphawallet.app.C;
Expand All @@ -34,7 +43,7 @@
import com.alphawallet.app.repository.KeyProviderFactory;
import com.alphawallet.app.repository.PreferenceRepositoryType;
import com.alphawallet.app.service.GasService;
import com.alphawallet.app.service.WalletConnectV2Service;
import com.alphawallet.app.ui.WalletConnectNotificationActivity;
import com.alphawallet.app.ui.WalletConnectSessionActivity;
import com.alphawallet.app.ui.WalletConnectV2Activity;
import com.alphawallet.app.ui.widget.entity.ActionSheetCallback;
Expand Down Expand Up @@ -86,6 +95,7 @@ public class AWWalletConnectClient implements Web3Wallet.WalletDelegate
private boolean hasConnection;
private Application application;
private final PreferenceRepositoryType preferenceRepository;
private final int WC_NOTIFICATION_ID = 25964950;

public AWWalletConnectClient(Context context, WalletConnectInteract walletConnectInteract, PreferenceRepositoryType preferenceRepository, GasService gasService)
{
Expand Down Expand Up @@ -270,18 +280,13 @@ private void updateService(List<WalletConnectSessionItem> items)
{
try
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (items.isEmpty())
{
if (items.isEmpty())
{
context.stopService(new Intent(context, WalletConnectV2Service.class));
//now signal
}
else
{
Intent serviceIntent = new Intent(context, WalletConnectV2Service.class);
ContextCompat.startForegroundService(context, serviceIntent);
}
removeNotification();
}
else
{
displayNotification();
}
}
catch (Exception e)
Expand All @@ -291,6 +296,63 @@ private void updateService(List<WalletConnectSessionItem> items)
}
}

public void displayNotification()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
createNotificationChannel();
}
Notification notification = createNotification();

// Issue the notification if we have user permission
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED)
{
notificationManager.notify(WC_NOTIFICATION_ID, notification);
}
else
{
Intent intent = new Intent(C.REQUEST_NOTIFICATION_ACCESS);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}

final String CHANNEL_ID = "WalletConnectV2Service";
private Notification createNotification()
{
Intent notificationIntent = new Intent(context, WalletConnectNotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, WC_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

return new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(context.getString(R.string.notify_wallet_connect_title))
.setContentText(context.getString(R.string.notify_wallet_connect_content))
.setSmallIcon(R.drawable.ic_logo)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build();
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel()
{
CharSequence name = context.getString(R.string.notify_wallet_connect_title);
String description = context.getString(R.string.notify_wallet_connect_content);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(description);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}

//remove notification
private void removeNotification()
{
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED)
{
notificationManager.cancel(WC_NOTIFICATION_ID);
}
}


public void reject(Model.SessionProposal sessionProposal, WalletConnectV2Callback callback)
{
Expand Down

0 comments on commit 07de70b

Please sign in to comment.