android-notify 1.1__tar.gz → 1.21__tar.gz
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-1.21/PKG-INFO +251 -0
- android_notify-1.21/README.md +238 -0
- android_notify-1.21/android_notify/__init__.py +2 -0
- android_notify-1.21/android_notify/core.py +132 -0
- android_notify-1.21/android_notify.egg-info/PKG-INFO +251 -0
- {android_notify-1.1 → android_notify-1.21}/setup.py +1 -1
- android_notify-1.1/PKG-INFO +0 -139
- android_notify-1.1/README.md +0 -127
- android_notify-1.1/android_notify/__init__.py +0 -0
- android_notify-1.1/android_notify/core.py +0 -168
- android_notify-1.1/android_notify.egg-info/PKG-INFO +0 -139
- {android_notify-1.1 → android_notify-1.21}/android_notify/__main__.py +0 -0
- {android_notify-1.1 → android_notify-1.21}/android_notify/styles.py +0 -0
- {android_notify-1.1 → android_notify-1.21}/android_notify.egg-info/SOURCES.txt +0 -0
- {android_notify-1.1 → android_notify-1.21}/android_notify.egg-info/dependency_links.txt +0 -0
- {android_notify-1.1 → android_notify-1.21}/android_notify.egg-info/requires.txt +0 -0
- {android_notify-1.1 → android_notify-1.21}/android_notify.egg-info/top_level.txt +0 -0
- {android_notify-1.1 → android_notify-1.21}/setup.cfg +0 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: android_notify
|
|
3
|
+
Version: 1.21
|
|
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
|
+
- Large Icon
|
|
25
|
+
- Inbox
|
|
26
|
+
- Supports including images in notifications.
|
|
27
|
+
- Compatible with Android 8.0+ (Notification Channels).
|
|
28
|
+
- Customizable notification channels.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
This package is available on PyPI and can be installed via pip:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install android-notify
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## **Dependencies**
|
|
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, androidx.core:core-ktx:1.15.0
|
|
56
|
+
android.enable_androidx = True
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
### Example Notification
|
|
62
|
+
|
|
63
|
+
#### Basic Notification
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from android_notify import send_notification
|
|
67
|
+
|
|
68
|
+
# Send a basic notification
|
|
69
|
+
send_notification("Hello", "This is a basic notification.")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Example Image:**
|
|
73
|
+

|
|
74
|
+
|
|
75
|
+
#### Notification with an Image (Big Picture Style)
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
# Send a notification with an image
|
|
79
|
+
send_notification(
|
|
80
|
+
title='Picture Alert!',
|
|
81
|
+
message='This notification includes an image.',
|
|
82
|
+
style='big_picture',
|
|
83
|
+
img_path='assets/imgs/icon.png'
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Example Image:**
|
|
88
|
+

|
|
89
|
+
|
|
90
|
+
#### Inbox Notification Style
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
# Send a notification with inbox style
|
|
94
|
+
send_notification(
|
|
95
|
+
title='Inbox Notification',
|
|
96
|
+
message='Line 1\nLine 2\nLine 3',
|
|
97
|
+
style='inbox'
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Example Image:**
|
|
102
|
+

|
|
103
|
+
|
|
104
|
+
#### Big Text Notification
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
# Send a Big Text notification
|
|
108
|
+
send_notification(
|
|
109
|
+
title='Hello!',
|
|
110
|
+
message='This is a sample notification.',
|
|
111
|
+
style='big_text'
|
|
112
|
+
)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**Example Image:**
|
|
116
|
+

|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
### **Assist**
|
|
121
|
+
|
|
122
|
+
- How to Copy image to app folder
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
import shutil,os # These module comes packaged with python
|
|
126
|
+
from android.storage import app_storage_path # type: ignore -- This works only on android
|
|
127
|
+
|
|
128
|
+
app_path = os.path.join(app_storage_path(),'app')
|
|
129
|
+
image_path= "/storage/emulated/0/Download/profile.png"
|
|
130
|
+
|
|
131
|
+
shutil.copy(image_path, os.path.join(app_path, "profile.png"))
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
- Avoiding Human Error when using different notification styles
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from android_notify import NotificationStyles
|
|
138
|
+
send_notification(
|
|
139
|
+
title='Picture Alert!',
|
|
140
|
+
message='This notification includes an image.',
|
|
141
|
+
img_path='assets/imgs/icon.png'
|
|
142
|
+
style=NotificationStyles.BIG_PICTURE,
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
### **Functions Reference**
|
|
149
|
+
|
|
150
|
+
### 1. `asks_permission_if_needed()`
|
|
151
|
+
|
|
152
|
+
**Description:**
|
|
153
|
+
|
|
154
|
+
- Checks if notification permissions are granted and requests them if missing.
|
|
155
|
+
|
|
156
|
+
**Usage:**
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
asks_permission_if_needed()
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
### 2. `get_image_uri(relative_path)`
|
|
165
|
+
|
|
166
|
+
**Description:**
|
|
167
|
+
|
|
168
|
+
- Resolves the absolute URI for an image in the app's storage.
|
|
169
|
+
|
|
170
|
+
**Parameters:**
|
|
171
|
+
|
|
172
|
+
- `relative_path` *(str)*: Path to the image (e.g., `assets/imgs/icon.png`).
|
|
173
|
+
|
|
174
|
+
**Returns:**
|
|
175
|
+
|
|
176
|
+
- `Uri`: Android URI object for the image.
|
|
177
|
+
|
|
178
|
+
**Usage:**
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
uri = get_image_uri('assets/imgs/icon.png')
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
### 3. `send_notification(title, message, style=None, img_path=None, channel_id='default_channel')`
|
|
187
|
+
|
|
188
|
+
**Description:**
|
|
189
|
+
|
|
190
|
+
- Sends an Android notification with optional styles and images.
|
|
191
|
+
|
|
192
|
+
**Parameters:**
|
|
193
|
+
|
|
194
|
+
- `title` *(str)*: Notification title.
|
|
195
|
+
- `message` *(str)*: Notification message.
|
|
196
|
+
- `style` *(str, optional)*: Notification style (`big_text`, `big_picture`, `inbox`, `large_icon`).
|
|
197
|
+
- `img_path` *(str, optional)*: Path to the image resource.(for `big_picture` or `large_icon` styles).
|
|
198
|
+
- `channel_id` *(str, optional)*: Notification channel ID.
|
|
199
|
+
|
|
200
|
+
Returns - notification id
|
|
201
|
+
|
|
202
|
+
### Advanced Usage
|
|
203
|
+
|
|
204
|
+
You can customize notification channels for different types of notifications.
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
send_notification(
|
|
208
|
+
title='Custom Channel Notification',
|
|
209
|
+
message='This uses a custom notification channel.',
|
|
210
|
+
channel_id='custom_channel'
|
|
211
|
+
)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Contribution
|
|
215
|
+
|
|
216
|
+
Feel free to open issues or submit pull requests for improvements!
|
|
217
|
+
|
|
218
|
+
## 🐛 Reporting Issues
|
|
219
|
+
|
|
220
|
+
Found a bug? Please open an issue on our [GitHub Issues](https://github.com/Fector101/android_notify/issues) page.
|
|
221
|
+
|
|
222
|
+
## Author
|
|
223
|
+
|
|
224
|
+
- Fabian - <fector101@yahoo.com>
|
|
225
|
+
- GitHub: <https://github.com/Fector101/android_notify>
|
|
226
|
+
- Twitter: <https://x.com/CodewithFabian> -- 😊 I'm sure to answer
|
|
227
|
+
|
|
228
|
+
For feedback or contributions, feel free to reach out!
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## ☕ Support the Project
|
|
233
|
+
|
|
234
|
+
If you find this project helpful, consider buying me a coffee! Your support helps maintain and improve the project.
|
|
235
|
+
|
|
236
|
+
<a href="https://www.buymeacoffee.com/fector101" target="_blank">
|
|
237
|
+
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="60">
|
|
238
|
+
</a>
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Acknowledgments
|
|
243
|
+
|
|
244
|
+
- Thanks to the Kivy and Pyjnius communities for their support.
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## 🌐 **Links**
|
|
249
|
+
|
|
250
|
+
- **PyPI:** [android-notify on PyPI](https://pypi.org/project/android-notify/)
|
|
251
|
+
- **GitHub:** [Source Code Repository](https://github.com/Fector101/android_notify/)
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# Android Notify
|
|
2
|
+
|
|
3
|
+
`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.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Send Android notifications with custom titles and messages.
|
|
8
|
+
- Support for multiple notification styles:
|
|
9
|
+
- Big Text
|
|
10
|
+
- Big Picture
|
|
11
|
+
- Large Icon
|
|
12
|
+
- Inbox
|
|
13
|
+
- Supports including images in notifications.
|
|
14
|
+
- Compatible with Android 8.0+ (Notification Channels).
|
|
15
|
+
- Customizable notification channels.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
This package is available on PyPI and can be installed via pip:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install android-notify
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## **Dependencies**
|
|
26
|
+
|
|
27
|
+
**Prerequisites:**
|
|
28
|
+
|
|
29
|
+
- Buildozer
|
|
30
|
+
- Kivy
|
|
31
|
+
|
|
32
|
+
In your **`buildozer.spec`** file, ensure you include the following:
|
|
33
|
+
|
|
34
|
+
```ini
|
|
35
|
+
# Add pyjnius so it's packaged with the build
|
|
36
|
+
requirements = python3,kivy,pyjnius
|
|
37
|
+
|
|
38
|
+
# Add permission for notifications
|
|
39
|
+
android.permissions = POST_NOTIFICATIONS
|
|
40
|
+
|
|
41
|
+
# Required dependencies (write exactly as shown, no quotation marks)
|
|
42
|
+
android.gradle_dependencies = androidx.core:core:1.6.0, androidx.core:core-ktx:1.15.0
|
|
43
|
+
android.enable_androidx = True
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
### Example Notification
|
|
49
|
+
|
|
50
|
+
#### Basic Notification
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from android_notify import send_notification
|
|
54
|
+
|
|
55
|
+
# Send a basic notification
|
|
56
|
+
send_notification("Hello", "This is a basic notification.")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**Example Image:**
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
#### Notification with an Image (Big Picture Style)
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
# Send a notification with an image
|
|
66
|
+
send_notification(
|
|
67
|
+
title='Picture Alert!',
|
|
68
|
+
message='This notification includes an image.',
|
|
69
|
+
style='big_picture',
|
|
70
|
+
img_path='assets/imgs/icon.png'
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**Example Image:**
|
|
75
|
+

|
|
76
|
+
|
|
77
|
+
#### Inbox Notification Style
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
# Send a notification with inbox style
|
|
81
|
+
send_notification(
|
|
82
|
+
title='Inbox Notification',
|
|
83
|
+
message='Line 1\nLine 2\nLine 3',
|
|
84
|
+
style='inbox'
|
|
85
|
+
)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Example Image:**
|
|
89
|
+

|
|
90
|
+
|
|
91
|
+
#### Big Text Notification
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# Send a Big Text notification
|
|
95
|
+
send_notification(
|
|
96
|
+
title='Hello!',
|
|
97
|
+
message='This is a sample notification.',
|
|
98
|
+
style='big_text'
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Example Image:**
|
|
103
|
+

|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### **Assist**
|
|
108
|
+
|
|
109
|
+
- How to Copy image to app folder
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import shutil,os # These module comes packaged with python
|
|
113
|
+
from android.storage import app_storage_path # type: ignore -- This works only on android
|
|
114
|
+
|
|
115
|
+
app_path = os.path.join(app_storage_path(),'app')
|
|
116
|
+
image_path= "/storage/emulated/0/Download/profile.png"
|
|
117
|
+
|
|
118
|
+
shutil.copy(image_path, os.path.join(app_path, "profile.png"))
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
- Avoiding Human Error when using different notification styles
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from android_notify import NotificationStyles
|
|
125
|
+
send_notification(
|
|
126
|
+
title='Picture Alert!',
|
|
127
|
+
message='This notification includes an image.',
|
|
128
|
+
img_path='assets/imgs/icon.png'
|
|
129
|
+
style=NotificationStyles.BIG_PICTURE,
|
|
130
|
+
)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
### **Functions Reference**
|
|
136
|
+
|
|
137
|
+
### 1. `asks_permission_if_needed()`
|
|
138
|
+
|
|
139
|
+
**Description:**
|
|
140
|
+
|
|
141
|
+
- Checks if notification permissions are granted and requests them if missing.
|
|
142
|
+
|
|
143
|
+
**Usage:**
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
asks_permission_if_needed()
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
### 2. `get_image_uri(relative_path)`
|
|
152
|
+
|
|
153
|
+
**Description:**
|
|
154
|
+
|
|
155
|
+
- Resolves the absolute URI for an image in the app's storage.
|
|
156
|
+
|
|
157
|
+
**Parameters:**
|
|
158
|
+
|
|
159
|
+
- `relative_path` *(str)*: Path to the image (e.g., `assets/imgs/icon.png`).
|
|
160
|
+
|
|
161
|
+
**Returns:**
|
|
162
|
+
|
|
163
|
+
- `Uri`: Android URI object for the image.
|
|
164
|
+
|
|
165
|
+
**Usage:**
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
uri = get_image_uri('assets/imgs/icon.png')
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
### 3. `send_notification(title, message, style=None, img_path=None, channel_id='default_channel')`
|
|
174
|
+
|
|
175
|
+
**Description:**
|
|
176
|
+
|
|
177
|
+
- Sends an Android notification with optional styles and images.
|
|
178
|
+
|
|
179
|
+
**Parameters:**
|
|
180
|
+
|
|
181
|
+
- `title` *(str)*: Notification title.
|
|
182
|
+
- `message` *(str)*: Notification message.
|
|
183
|
+
- `style` *(str, optional)*: Notification style (`big_text`, `big_picture`, `inbox`, `large_icon`).
|
|
184
|
+
- `img_path` *(str, optional)*: Path to the image resource.(for `big_picture` or `large_icon` styles).
|
|
185
|
+
- `channel_id` *(str, optional)*: Notification channel ID.
|
|
186
|
+
|
|
187
|
+
Returns - notification id
|
|
188
|
+
|
|
189
|
+
### Advanced Usage
|
|
190
|
+
|
|
191
|
+
You can customize notification channels for different types of notifications.
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
send_notification(
|
|
195
|
+
title='Custom Channel Notification',
|
|
196
|
+
message='This uses a custom notification channel.',
|
|
197
|
+
channel_id='custom_channel'
|
|
198
|
+
)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Contribution
|
|
202
|
+
|
|
203
|
+
Feel free to open issues or submit pull requests for improvements!
|
|
204
|
+
|
|
205
|
+
## 🐛 Reporting Issues
|
|
206
|
+
|
|
207
|
+
Found a bug? Please open an issue on our [GitHub Issues](https://github.com/Fector101/android_notify/issues) page.
|
|
208
|
+
|
|
209
|
+
## Author
|
|
210
|
+
|
|
211
|
+
- Fabian - <fector101@yahoo.com>
|
|
212
|
+
- GitHub: <https://github.com/Fector101/android_notify>
|
|
213
|
+
- Twitter: <https://x.com/CodewithFabian> -- 😊 I'm sure to answer
|
|
214
|
+
|
|
215
|
+
For feedback or contributions, feel free to reach out!
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## ☕ Support the Project
|
|
220
|
+
|
|
221
|
+
If you find this project helpful, consider buying me a coffee! Your support helps maintain and improve the project.
|
|
222
|
+
|
|
223
|
+
<a href="https://www.buymeacoffee.com/fector101" target="_blank">
|
|
224
|
+
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="60">
|
|
225
|
+
</a>
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Acknowledgments
|
|
230
|
+
|
|
231
|
+
- Thanks to the Kivy and Pyjnius communities for their support.
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## 🌐 **Links**
|
|
236
|
+
|
|
237
|
+
- **PyPI:** [android-notify on PyPI](https://pypi.org/project/android-notify/)
|
|
238
|
+
- **GitHub:** [Source Code Repository](https://github.com/Fector101/android_notify/)
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
|
|
2
|
+
from jnius import autoclass,cast
|
|
3
|
+
import random
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
ON_ANDROID = False
|
|
7
|
+
try:
|
|
8
|
+
# Get the required Java classes
|
|
9
|
+
PythonActivity = autoclass('org.kivy.android.PythonActivity')
|
|
10
|
+
NotificationChannel = autoclass('android.app.NotificationChannel')
|
|
11
|
+
String = autoclass('java.lang.String')
|
|
12
|
+
Intent = autoclass('android.content.Intent')
|
|
13
|
+
PendingIntent = autoclass('android.app.PendingIntent')
|
|
14
|
+
context = PythonActivity.mActivity # Get the app's context
|
|
15
|
+
BitmapFactory = autoclass('android.graphics.BitmapFactory')
|
|
16
|
+
BuildVersion = autoclass('android.os.Build$VERSION')
|
|
17
|
+
ON_ANDROID=True
|
|
18
|
+
except Exception as e:
|
|
19
|
+
print('This Package Only Runs on Android !!! ---> Check "https://github.com/Fector101/android_notify/" to see design patterns and more info.')
|
|
20
|
+
|
|
21
|
+
if ON_ANDROID:
|
|
22
|
+
try:
|
|
23
|
+
NotificationManagerCompat = autoclass('androidx.core.app.NotificationManagerCompat')
|
|
24
|
+
NotificationCompat = autoclass('androidx.core.app.NotificationCompat')
|
|
25
|
+
|
|
26
|
+
# Notification Design
|
|
27
|
+
NotificationCompatBuilder = autoclass('androidx.core.app.NotificationCompat$Builder')
|
|
28
|
+
NotificationCompatBigTextStyle = autoclass('androidx.core.app.NotificationCompat$BigTextStyle')
|
|
29
|
+
# NotificationCompatBigTextStyle = autoclass('android.app.Notification$BigTextStyle')
|
|
30
|
+
NotificationCompatBigPictureStyle = autoclass('androidx.core.app.NotificationCompat$BigPictureStyle')
|
|
31
|
+
NotificationCompatInboxStyle = autoclass('androidx.core.app.NotificationCompat$InboxStyle')
|
|
32
|
+
except Exception as e:
|
|
33
|
+
print("""
|
|
34
|
+
Dependency Error: Add the following in buildozer.spec:
|
|
35
|
+
* android.gradle_dependencies = androidx.core:core-ktx:1.15.0, androidx.core:core:1.6.0
|
|
36
|
+
* android.enable_androidx = True
|
|
37
|
+
* android.permissions = POST_NOTIFICATIONS
|
|
38
|
+
""")
|
|
39
|
+
|
|
40
|
+
def asks_permission_if_needed():
|
|
41
|
+
"""
|
|
42
|
+
Ask for permission to send notifications if needed.
|
|
43
|
+
"""
|
|
44
|
+
from android.permissions import request_permissions, Permission,check_permission # type: ignore
|
|
45
|
+
|
|
46
|
+
permissions=[Permission.POST_NOTIFICATIONS]
|
|
47
|
+
if not all(check_permission(p) for p in permissions):
|
|
48
|
+
request_permissions(permissions)
|
|
49
|
+
|
|
50
|
+
def get_image_uri(relative_path):
|
|
51
|
+
"""
|
|
52
|
+
Get the absolute URI for an image in the assets folder.
|
|
53
|
+
:param relative_path: The relative path to the image (e.g., 'assets/imgs/icon.png').
|
|
54
|
+
:return: Absolute URI java Object (e.g., 'file:///path/to/file.png').
|
|
55
|
+
"""
|
|
56
|
+
from android.storage import app_storage_path # type: ignore
|
|
57
|
+
|
|
58
|
+
output_path = os.path.join(app_storage_path(),'app', relative_path)
|
|
59
|
+
# print(output_path,'output_path') # /data/user/0/org.laner.lan_ft/files/app/assets/imgs/icon.png
|
|
60
|
+
|
|
61
|
+
if not os.path.exists(output_path):
|
|
62
|
+
raise FileNotFoundError(f"Image not found at path: {output_path}")
|
|
63
|
+
|
|
64
|
+
Uri = autoclass('android.net.Uri')
|
|
65
|
+
return Uri.parse(f"file://{output_path}")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def send_notification(title:str, message:str, style=None, img_path=None, channel_id:str="default_channel"):
|
|
69
|
+
"""
|
|
70
|
+
Send a notification on Android.
|
|
71
|
+
|
|
72
|
+
:param title: Title of the notification.
|
|
73
|
+
:param message: Message body.
|
|
74
|
+
:param style: Style of the notification ('big_text', 'big_picture', 'inbox', 'large_icon').
|
|
75
|
+
:param img_path: Path to the image resource.
|
|
76
|
+
:param channel_id: Notification channel ID.
|
|
77
|
+
"""
|
|
78
|
+
if not ON_ANDROID:
|
|
79
|
+
print('This Package Only Runs on Android !!! ---> Check "https://github.com/Fector101/android_notify/" for Documentation.')
|
|
80
|
+
return
|
|
81
|
+
|
|
82
|
+
# Get notification manager
|
|
83
|
+
notification_manager = context.getSystemService(context.NOTIFICATION_SERVICE)
|
|
84
|
+
|
|
85
|
+
# importance= autoclass('android.app.NotificationManager').IMPORTANCE_HIGH # also works #NotificationManager.IMPORTANCE_DEFAULT
|
|
86
|
+
importance= NotificationManagerCompat.IMPORTANCE_HIGH #autoclass('android.app.NotificationManager').IMPORTANCE_HIGH also works #NotificationManager.IMPORTANCE_DEFAULT
|
|
87
|
+
|
|
88
|
+
# Notification Channel (Required for Android 8.0+)
|
|
89
|
+
if BuildVersion.SDK_INT >= 26:
|
|
90
|
+
channel = NotificationChannel(channel_id, "Default Channel",importance)
|
|
91
|
+
notification_manager.createNotificationChannel(channel)
|
|
92
|
+
|
|
93
|
+
# Build the notification
|
|
94
|
+
builder = NotificationCompatBuilder(context, channel_id)
|
|
95
|
+
builder.setContentTitle(title)
|
|
96
|
+
builder.setContentText(message)
|
|
97
|
+
builder.setSmallIcon(context.getApplicationInfo().icon)
|
|
98
|
+
builder.setDefaults(NotificationCompat.DEFAULT_ALL)
|
|
99
|
+
builder.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
100
|
+
|
|
101
|
+
img=None
|
|
102
|
+
if img_path:
|
|
103
|
+
try:
|
|
104
|
+
img = get_image_uri(img_path)
|
|
105
|
+
except FileNotFoundError as e:
|
|
106
|
+
print('Failed Adding Bitmap: ',e)
|
|
107
|
+
|
|
108
|
+
# Apply notification styles
|
|
109
|
+
try:
|
|
110
|
+
if style == "big_text":
|
|
111
|
+
big_text_style = NotificationCompatBigTextStyle()
|
|
112
|
+
big_text_style.bigText(message)
|
|
113
|
+
builder.setStyle(big_text_style)
|
|
114
|
+
elif style == "big_picture" and img_path:
|
|
115
|
+
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(img))
|
|
116
|
+
builder.setLargeIcon(bitmap)
|
|
117
|
+
big_picture_style = NotificationCompatBigPictureStyle().bigPicture(bitmap)
|
|
118
|
+
builder.setStyle(big_picture_style)
|
|
119
|
+
elif style == "inbox":
|
|
120
|
+
inbox_style = NotificationCompatInboxStyle()
|
|
121
|
+
for line in message.split("\n"):
|
|
122
|
+
inbox_style.addLine(line)
|
|
123
|
+
builder.setStyle(inbox_style)
|
|
124
|
+
elif style == "large_icon" and img_path:
|
|
125
|
+
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(img))
|
|
126
|
+
builder.setLargeIcon(bitmap)
|
|
127
|
+
except Exception as e:
|
|
128
|
+
print('Failed Adding Style: ',e)
|
|
129
|
+
# Display the notification
|
|
130
|
+
notification_id = random.randint(0, 100)
|
|
131
|
+
notification_manager.notify(notification_id, builder.build())
|
|
132
|
+
return notification_id
|