88 get_android_importance , generate_channel_id , get_img_from_path , setLayoutText , \
99 get_bitmap_from_url , add_data_to_intent
1010
11- from .config import from_service_file , get_python_activity ,get_notification_manager ,ON_ANDROID
11+ from .config import from_service_file , get_python_activity ,get_notification_manager ,ON_ANDROID , on_flet_app
1212from .config import (Bundle , String , BuildVersion ,
1313 Intent ,PendingIntent ,
1414 IconCompat ,app_storage_path ,
1515 NotificationChannel ,RemoteViews ,
16- run_on_ui_thread ,android_activity ,
17- request_permissions , Permission ,check_permission
16+ run_on_ui_thread ,
1817 )
1918from .config import (NotificationCompat , NotificationCompatBuilder ,
2019 NotificationCompatBigTextStyle ,NotificationCompatBigPictureStyle ,
2120 NotificationCompatInboxStyle , NotificationCompatDecoratedCustomViewStyle ,
22- Color
21+ Color
2322 )
2423from .styles import NotificationStyles
2524from .base import BaseNotification
@@ -622,7 +621,7 @@ def __dispatch_notification(self):
622621 self .notification_manager .notify (self .__id , self .__builder .build ())
623622 except Exception as sending_notification_from_service_error :
624623 print ('error sending notification from service:' ,sending_notification_from_service_error )
625- elif self .passed_check or NotificationHandler .has_permission ():
624+ elif on_flet_app () or self .passed_check or NotificationHandler .has_permission ():
626625 try :
627626 self .notification_manager .notify (self .__id , self .__builder .build ())
628627 except Exception as notify_error :
@@ -852,6 +851,10 @@ class NotificationHandler:
852851 __name = None
853852 __bound = False
854853 __requesting_permission = False
854+ android_activity = None
855+ if ON_ANDROID and not on_flet_app ():
856+ from android import activity
857+ android_activity = activity
855858 @classmethod
856859 def get_name (cls ):
857860 """Returns name or id str for Clicked Notification."""
@@ -881,7 +884,7 @@ def get_name(cls):
881884 def __notification_handler (cls , intent ):
882885 """Calls Function Attached to notification on click.
883886 Don't Call this function manual, it's Already Attach to Notification.
884-
887+
885888 Sets self.__name #action of Notification that was clicked from Notification.name or Notification.id
886889 """
887890 if not cls .is_on_android ():
@@ -915,14 +918,17 @@ def __notification_handler(cls, intent):
915918 @classmethod
916919 def bindNotifyListener (cls ):
917920 """This Creates a Listener for All Notification Clicks and Functions"""
921+ if on_flet_app ():
922+ return False
923+
918924 if not cls .is_on_android ():
919925 return "Not on Android"
920926 #TODO keep trying BroadcastReceiver
921927 if cls .__bound :
922928 print ("binding done already " )
923929 return True
924930 try :
925- android_activity .bind (on_new_intent = cls .__notification_handler )
931+ cls . android_activity .bind (on_new_intent = cls .__notification_handler )
926932 cls .__bound = True
927933 return True
928934 except Exception as binding_listener_error :
@@ -936,10 +942,10 @@ def unbindNotifyListener(cls):
936942 return "Not on Android"
937943
938944 #Beta TODO use BroadcastReceiver
939- if from_service_file ():
945+ if on_flet_app () or from_service_file ():
940946 return False # error 'NoneType' object has no attribute 'registerNewIntentListener'
941947 try :
942- android_activity .unbind (on_new_intent = cls .__notification_handler )
948+ cls . android_activity .unbind (on_new_intent = cls .__notification_handler )
943949 return True
944950 except Exception as unbinding_listener_error :
945951 print ("Failed to unbind notifications listener: " ,unbinding_listener_error )
@@ -958,7 +964,22 @@ def has_permission():
958964 """
959965 if not ON_ANDROID :
960966 return True
961- return check_permission (Permission .POST_NOTIFICATIONS )
967+
968+ if on_flet_app ():
969+ from .config import autoclass
970+ ContextCompat = autoclass ('androidx.core.content.ContextCompat' )
971+ Manifest = autoclass ('android.Manifest$permission' )
972+ VERSION_CODES = autoclass ('android.os.Build$VERSION_CODES' )
973+
974+ if BuildVersion .SDK_INT >= VERSION_CODES .TIRAMISU :
975+ permission = Manifest .POST_NOTIFICATIONS
976+ return ContextCompat .checkSelfPermission (context , permission )
977+ else :
978+ print ("android_notify- On Low android version don't need permission" )
979+ return True #doesn't need permission
980+ else :
981+ from android .permissions import Permission , check_permission
982+ return check_permission (Permission .POST_NOTIFICATIONS )
962983
963984 @classmethod
964985 @run_on_ui_thread
@@ -984,23 +1005,35 @@ def on_permissions_result(permissions, grants):
9841005 cls .__requesting_permission = False
9851006
9861007 if not cls .has_permission ():
987- cls .__requesting_permission = True
988- request_permissions ([Permission .POST_NOTIFICATIONS ],on_permissions_result )
1008+ if on_flet_app ():
1009+ from .config import autoclass
1010+ ActivityCompat = autoclass ('androidx.core.app.ActivityCompat' )
1011+ Manifest = autoclass ('android.Manifest$permission' )
1012+ permission = Manifest .POST_NOTIFICATIONS
1013+ ActivityCompat .requestPermissions (context , [permission ], 101 )
1014+ return None
1015+ # TODO Callback when user answers request question
1016+ else :
1017+ from android .permissions import request_permissions , Permission
1018+ cls .__requesting_permission = True
1019+ request_permissions ([Permission .POST_NOTIFICATIONS ],on_permissions_result )
1020+ return None
9891021 else :
9901022 cls .__requesting_permission = False
9911023 if callback :
9921024 if can_accept_arguments (callback ,True ):
9931025 callback (True )
9941026 else :
9951027 callback ()
1028+ return None
9961029
9971030
998- if not from_service_file ():
1031+ if not on_flet_app () and from_service_file ():
1032+ print ("didn't bind listener, In service file" )
1033+ elif ON_ANDROID :
9991034 try :
10001035 NotificationHandler .bindNotifyListener ()
10011036 except Exception as bind_error :
1037+ # error 'NoneType' object has no attribute 'registerNewIntentListener'
10021038 print ("notification listener bind error:" ,bind_error )
1003- traceback .print_exc ()
1004- else :
1005- # error 'NoneType' object has no attribute 'registerNewIntentListener'
1006- print ("didn't bind listener, In service file:" )
1039+ traceback .print_exc ()
0 commit comments