android-notify 0.2__py3-none-any.whl → 1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of android-notify might be problematic. Click here for more details.

android_notify/core.py CHANGED
@@ -1,38 +1,41 @@
1
- from jnius import autoclass
1
+
2
+ from jnius import autoclass,cast
2
3
  import random
3
- from jnius import autoclass
4
4
  import os
5
- import shutil
5
+
6
+
7
+ def asks_permission_if_needed():
8
+ """
9
+ Ask for permission to send notifications if needed.
10
+ """
11
+ # Get the required Java classes
12
+ from android.permissions import request_permissions, Permission,check_permission # type: ignore
13
+
14
+ def check_permissions(permissions):
15
+ for permission in permissions:
16
+ if check_permission(permission) != True:
17
+ return False
18
+ return True
19
+
20
+ permissions=[Permission.POST_NOTIFICATIONS]
21
+ if check_permissions(permissions):
22
+ request_permissions(permissions)
6
23
 
7
24
  def get_image_uri(relative_path):
8
25
  """
9
26
  Get the absolute URI for an image in the assets folder.
10
27
  :param relative_path: The relative path to the image (e.g., 'assets/imgs/icon.png').
11
- :return: Absolute URI (e.g., 'file:///path/to/file.png').
28
+ :return: Absolute URI java Object (e.g., 'file:///path/to/file.png').
12
29
  """
13
- # Access Android's context and asset manager
14
- PythonActivity = autoclass('org.kivy.android.PythonActivity')
15
- context = PythonActivity.mActivity
16
- asset_manager = context.getAssets()
30
+ from android.storage import app_storage_path # type: ignore
31
+ # print("app_storage_path()",app_storage_path())
17
32
 
18
- # Construct the full path for the output file in cache directory
19
- cache_dir = context.getCacheDir().getAbsolutePath()
20
- file_name = os.path.basename(relative_path)
21
- output_path = os.path.join(cache_dir, file_name)
33
+ output_path = os.path.join(app_storage_path(),'app', relative_path)
34
+ # print(output_path,'output_path') # /data/user/0/org.laner.lan_ft/files/app/assets/imgs/icon.png
22
35
 
23
- # Copy the file from assets to cache directory
24
- with asset_manager.open(relative_path) as asset_file:
25
- with open(output_path, 'wb') as output_file:
26
- shutil.copyfileobj(asset_file, output_file)
27
-
28
- # Return the URI
29
36
  Uri = autoclass('android.net.Uri')
30
37
  return Uri.parse(f"file://{output_path}")
31
38
 
32
- # # Example usage
33
- # image_uri = get_image_uri("imgs/icon.png")
34
- # print(image_uri)
35
-
36
39
  def send_notification(title, message, style=None, img_path=None, channel_id="default_channel"):
37
40
  """
38
41
  Send a notification on Android.
@@ -43,26 +46,43 @@ def send_notification(title, message, style=None, img_path=None, channel_id="def
43
46
  :param image: Image URL or drawable for 'big_picture' style.
44
47
  :param channel_id: Notification channel ID.
45
48
  """
49
+ # TODO check if running on android short circuit and return message if not
50
+
46
51
  # Get the required Java classes
52
+ # Notification Base
47
53
  PythonActivity = autoclass('org.kivy.android.PythonActivity')
48
- NotificationManager = autoclass('android.app.NotificationManager')
49
54
  NotificationChannel = autoclass('android.app.NotificationChannel')
55
+ String = autoclass('java.lang.String')
56
+
57
+
58
+ NotificationManagerCompat = autoclass('androidx.core.app.NotificationManagerCompat')
59
+ NotificationCompat = autoclass('androidx.core.app.NotificationCompat')
60
+
61
+ # Notification Design
50
62
  NotificationCompatBuilder = autoclass('androidx.core.app.NotificationCompat$Builder')
51
63
  NotificationCompatBigTextStyle = autoclass('androidx.core.app.NotificationCompat$BigTextStyle')
64
+ # NotificationCompatBigTextStyle = autoclass('android.app.Notification$BigTextStyle')
65
+
52
66
  NotificationCompatBigPictureStyle = autoclass('androidx.core.app.NotificationCompat$BigPictureStyle')
53
67
  NotificationCompatInboxStyle = autoclass('androidx.core.app.NotificationCompat$InboxStyle')
54
68
  BitmapFactory = autoclass('android.graphics.BitmapFactory')
55
-
69
+ BuildVersion = autoclass('android.os.Build$VERSION')
70
+ PendingIntent = autoclass('android.app.PendingIntent')
71
+ Intent = autoclass('android.content.Intent')
72
+
56
73
  # Get the app's context and notification manager
57
74
  context = PythonActivity.mActivity
58
75
  notification_manager = context.getSystemService(context.NOTIFICATION_SERVICE)
59
76
 
77
+ importance= NotificationManagerCompat.IMPORTANCE_HIGH #autoclass('android.app.NotificationManager').IMPORTANCE_HIGH also works #NotificationManager.IMPORTANCE_DEFAULT
78
+
60
79
  # Notification Channel (Required for Android 8.0+)
61
- if notification_manager.getNotificationChannel(channel_id) is None:
80
+ if BuildVersion.SDK_INT >= 26:
81
+ print('entered....')
62
82
  channel = NotificationChannel(
63
83
  channel_id,
64
84
  "Default Channel",
65
- NotificationManager.IMPORTANCE_DEFAULT
85
+ importance
66
86
  )
67
87
  notification_manager.createNotificationChannel(channel)
68
88
 
@@ -71,30 +91,78 @@ def send_notification(title, message, style=None, img_path=None, channel_id="def
71
91
  builder.setContentTitle(title)
72
92
  builder.setContentText(message)
73
93
  builder.setSmallIcon(context.getApplicationInfo().icon)
74
-
94
+ builder.setDefaults(NotificationCompat.DEFAULT_ALL)
95
+ builder.setPriority(NotificationCompat.PRIORITY_HIGH)
75
96
 
76
97
  # Get Image
98
+ img=img_path
77
99
  if img_path:
78
100
  try:
79
- img_path = get_image_uri(img_path)
101
+ img = get_image_uri(img_path)
80
102
  except Exception as e:
81
103
  print('Failed getting Image path',e)
82
104
 
105
+ # Add Actions (Buttons)
106
+
107
+ # add Action 1 Button
108
+ # try:
109
+ # # Create Action 1
110
+ # action_intent = Intent(context, PythonActivity)
111
+ # action_intent.setAction("ACTION_ONE")
112
+ # pending_action_intent = PendingIntent.getActivity(
113
+ # context,
114
+ # 0,
115
+ # action_intent,
116
+ # PendingIntent.FLAG_IMMUTABLE
117
+ # )
118
+
119
+ # # Convert text to CharSequence
120
+ # action_text = cast('java.lang.CharSequence', String("Action 1"))
121
+
122
+ # # Add action with proper types
123
+ # builder.addAction(
124
+ # int(context.getApplicationInfo().icon), # Cast icon to int
125
+ # action_text, # CharSequence text
126
+ # pending_action_intent # PendingIntent
127
+ # )
128
+
129
+
130
+ # # Set content intent for notification tap
131
+ # builder.setContentIntent(pending_action_intent)
132
+ # except Exception as e:
133
+ # print('Failed adding Action 1',e)
134
+
135
+
83
136
  # Apply styles
84
137
  if style == "big_text":
85
138
  big_text_style = NotificationCompatBigTextStyle()
86
139
  big_text_style.bigText(message)
87
140
  builder.setStyle(big_text_style)
141
+
142
+
88
143
  elif style == "big_picture" and img_path:
89
- bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(img_path))
90
- big_picture_style = NotificationCompatBigPictureStyle()
91
- big_picture_style.bigPicture(bitmap)
92
- builder.setStyle(big_picture_style)
144
+ try:
145
+ bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(img))
146
+ # bitmap = BitmapFactory.decodeFile(img_path)
147
+ builder.setLargeIcon(bitmap)
148
+ big_picture_style = NotificationCompatBigPictureStyle().bigPicture(bitmap)
149
+ # big_picture_style.bigPicture(bitmap).bigLargeIcon(None)
150
+ # big_picture_style.bigLargeIcon(bitmap) # This just changes dropdown app icon
151
+
152
+ builder.setStyle(big_picture_style)
153
+ except Exception as e:
154
+ print('Failed Adding Bitmap...', e)
93
155
  elif style == "inbox":
94
156
  inbox_style = NotificationCompatInboxStyle()
95
157
  for line in message.split("\n"):
96
158
  inbox_style.addLine(line)
97
159
  builder.setStyle(inbox_style)
98
-
160
+ elif style == "large_icon" and img_path:
161
+ try:
162
+ bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(img))
163
+ builder.setLargeIcon(bitmap)
164
+ except Exception as e:
165
+ print('Failed Large Icon...', e)
166
+
99
167
  # Show the notification
100
168
  notification_manager.notify(random.randint(0, 100), builder.build())
android_notify/styles.py CHANGED
@@ -2,3 +2,4 @@ class NotificationStyles:
2
2
  BIG_TEXT = "big_text"
3
3
  BIG_PICTURE = "big_picture"
4
4
  INBOX = "inbox"
5
+ LARGE_ICON = "large_icon"
@@ -0,0 +1,140 @@
1
+ Metadata-Version: 2.1
2
+ Name: android-notify
3
+ Version: 1.0
4
+ Summary: A Python package for sending Android notifications.
5
+ Home-page: https://github.com/Fector101/android_notify/
6
+ Author: Fabian
7
+ Author-email: fabianjoseph063@gmail.com
8
+ Project-URL: Funding, https://buymeacoffee.com/fector101
9
+ Project-URL: Source, https://github.com/Fector101/android_notify/
10
+ Requires-Python: >=3.6
11
+ Description-Content-Type: text/markdown
12
+ Requires-Dist: pyjnius
13
+
14
+ # Android Notify
15
+
16
+ `android_notify` is a Python module designed to simplify sending Android notifications using Kivy and Pyjnius. It supports multiple notification styles, including text, images, and inbox layouts.
17
+
18
+ ## Features
19
+
20
+ - Send Android notifications with custom titles and messages.
21
+ - Support for multiple notification styles:
22
+ - Big Text
23
+ - Big Picture
24
+ - Inbox
25
+ - Ability to include images in notifications.
26
+ - Compatible with Android 8.0+ (Notification Channels).
27
+ - Customizable notification channels.
28
+ - Support for large icons in notifications.
29
+
30
+ ## Installation
31
+
32
+ Make sure you have the required dependencies installed:
33
+
34
+ ```bash
35
+ pip install android-notify
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ **Prerequisites:**
41
+
42
+ - Buildozer
43
+ - Kivy
44
+
45
+ In your **`buildozer.spec`** file, ensure you include the following:
46
+
47
+ ```ini
48
+ # Add pyjnius so it's packaged with the build
49
+ requirements = python3,kivy,pyjnius
50
+
51
+ # Add permission for notifications
52
+ android.permissions = POST_NOTIFICATIONS
53
+
54
+ # Required dependencies (write exactly as shown, no quotation marks)
55
+ android.gradle_dependencies = androidx.core:core:1.6.0
56
+ android.enable_androidx = True
57
+ ```
58
+
59
+ ### Example Notification
60
+
61
+ ```python
62
+ from android_notify.core import send_notification
63
+
64
+ # Send a basic notification
65
+ send_notification("Hello", "This is a basic notification.")
66
+
67
+ # Send a notification with an image
68
+ send_notification(
69
+ title='Picture Alert!',
70
+ message='This notification includes an image.',
71
+ style='big_picture',
72
+ img_path='assets/imgs/icon.png'
73
+ )
74
+
75
+ # Send a notification with inbox style
76
+ send_notification(
77
+ title='Inbox Notification',
78
+ message='Line 1\nLine 2\nLine 3',
79
+ style='inbox'
80
+ )
81
+
82
+ # Send a Big Text notification (Note this send as a normal notification if not supported on said device)
83
+ send_notification(
84
+ title='Hello!',
85
+ message='This is a sample notification.',
86
+ style='big_text'
87
+ )
88
+ ```
89
+
90
+ ### Function Reference
91
+
92
+ #### `send_notification`
93
+
94
+ - **title** (*str*): Notification title.
95
+ - **message** (*str*): Notification message body.
96
+ - **style** (*str*): Notification style (`big_text`, `big_picture`, `inbox`, `large_icon`).
97
+ - **img_path** (*str*): Path to the image (for `big_picture` or `large_icon` styles).
98
+ - **channel_id** (*str*): Notification channel ID.
99
+
100
+ #### `get_image_uri`
101
+
102
+ - Resolves the absolute URI of an image resource.
103
+ - **relative_path** (*str*): The relative path to the image.
104
+
105
+ ### Advanced Usage
106
+
107
+ You can customize notification channels for different types of notifications.
108
+
109
+ ```python
110
+ send_notification(
111
+ title='Custom Channel Notification',
112
+ message='This uses a custom notification channel.',
113
+ channel_id='custom_channel'
114
+ )
115
+ ```
116
+
117
+ ## Contribution
118
+
119
+ Feel free to open issues or submit pull requests for improvements!
120
+
121
+ ## 🐛 Reporting Issues
122
+
123
+ Found a bug? Please open an issue on our [GitHub Issues](https://github.com/Fector101/android_notify/issues) page.
124
+
125
+ ## ☕ Support the Project
126
+
127
+ If you find TurboTask helpful, consider buying me a coffee! Your support helps maintain and improve the project.
128
+
129
+ <a href="https://www.buymeacoffee.com/fector101" target="_blank">
130
+ <img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="60">
131
+ </a>
132
+
133
+ ## Author
134
+
135
+ - Fabian - <fector101@yahoo.com>
136
+ - GitHub: <https://github.com/Fector101/android_notify>
137
+
138
+ ## Acknowledgments
139
+
140
+ - Thanks to the Kivy and Pyjnius communities for their support.
@@ -0,0 +1,8 @@
1
+ android_notify/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ android_notify/__main__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ android_notify/core.py,sha256=0edRXIF6ltTboJtkGjnOhLJDrYCYa2fzrB7cy61Y1PM,6598
4
+ android_notify/styles.py,sha256=P_8sAqb3Hbf_vbhqSoCVjKeqJ05Fr_CksO-HX5pj8pU,134
5
+ android_notify-1.0.dist-info/METADATA,sha256=JWHHKNPgCHy74RUARwmMwEW9usoW5gucc6Zf7XSO-3A,3805
6
+ android_notify-1.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
7
+ android_notify-1.0.dist-info/top_level.txt,sha256=IR1ONMrRSRINZpWn2X0dL5gbWwWINsK7PW8Jy2p4fU8,15
8
+ android_notify-1.0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: android-notify
3
- Version: 0.2
4
- Summary: A Python package for sending Android notifications.
5
- Requires-Dist: pyjnius
6
-
@@ -1,8 +0,0 @@
1
- android_notify/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- android_notify/__main__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- android_notify/core.py,sha256=lzwWcrcBXpDuZC6AQUo26NgHxDW9FFwTcS1DWFH_4Gs,4022
4
- android_notify/styles.py,sha256=OJBh-a2d_iOPcv50n_wUXezmh8Oc9MKDwM7c-RjkLAI,104
5
- android_notify-0.2.dist-info/METADATA,sha256=jPtCK-lTYxk8MjCJ0ipqLa61_S3efI8aRbpoVxST2K4,141
6
- android_notify-0.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
7
- android_notify-0.2.dist-info/top_level.txt,sha256=IR1ONMrRSRINZpWn2X0dL5gbWwWINsK7PW8Jy2p4fU8,15
8
- android_notify-0.2.dist-info/RECORD,,