55import static com .walletconnect .web3 .wallet .client .Wallet .Model ;
66import static com .walletconnect .web3 .wallet .client .Wallet .Params ;
77
8+ import android .Manifest ;
89import android .app .Activity ;
910import android .app .Application ;
11+ import android .app .Notification ;
12+ import android .app .NotificationChannel ;
13+ import android .app .NotificationManager ;
14+ import android .app .PendingIntent ;
1015import android .content .Context ;
1116import android .content .Intent ;
12- import android .content .pm .ServiceInfo ;
17+ import android .content .pm .PackageManager ;
1318import android .os .Build ;
1419import android .os .Handler ;
1520import android .os .Looper ;
1823
1924import androidx .activity .result .ActivityResultLauncher ;
2025import 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 ;
2230import androidx .lifecycle .MutableLiveData ;
31+ import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
2332
2433import com .alphawallet .app .App ;
2534import com .alphawallet .app .C ;
3443import com .alphawallet .app .repository .KeyProviderFactory ;
3544import com .alphawallet .app .repository .PreferenceRepositoryType ;
3645import com .alphawallet .app .service .GasService ;
37- import com .alphawallet .app .service . WalletConnectV2Service ;
46+ import com .alphawallet .app .ui . WalletConnectNotificationActivity ;
3847import com .alphawallet .app .ui .WalletConnectSessionActivity ;
3948import com .alphawallet .app .ui .WalletConnectV2Activity ;
4049import 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