-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (77 loc) · 3.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
from io import StringIO
from utils import EndPt, create_jwt_token, create_payload, send_push_notification
st.set_page_config(
page_title="APNs Tester",
page_icon=":material/mark_chat_unread:",
)
st.markdown(
"""
### :material/mark_chat_unread: Apple Push Notification Tester
> test Apple Push Notification with ease
![github](https://api.iconify.design/bi/github.svg?color=%236FD886&width=20)
[source code](https://github.com/hoishing/apn-tester)
"""
)
st.write("")
st.write("#### APNs Configuration")
c1, c2 = st.columns(2)
bundle_id = c1.text_input("App Bundle ID")
team_id = c2.text_input(
"Apple Team ID", help="see Apple Developer website → Membership details"
)
c3, c4 = st.columns(2)
key_id = c4.text_input(
"APNs Key ID",
help="see Apple Developer website → Certificates, IDs & Profiles → Keys",
)
endpoint = c3.radio(
"APNs Server",
options=EndPt._member_names_,
horizontal=True,
help="see the [docs](https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns#Establish-a-connection-to-APNs) for details",
)
device_tokens = st.text_area(
"Device Token(s)",
placeholder="accept multiple tokens, one token per line",
help="""
below is the code snippet to get the device token in string, see the [docs] for details
```swift
// Get the device token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let pushToken = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("Device Token: \(pushToken)")
}
```
[docs]: https://developer.apple.com/documentation/usernotifications/registering-your-app-with-apns
""",
)
uploaded_file = st.file_uploader(
"APNs Auth Key file(.p8)",
type="p8",
help="the `.p8` private key file generated by Apple when registering a new APNs key",
)
if uploaded_file:
auth_key = StringIO(uploaded_file.getvalue().decode("utf-8")).read()
st.write("#### Payload Content")
c5, c6 = st.columns(2)
title = c5.text_input("Title", value="Hi there! 👋")
subtitle = c6.text_input("Subtitle", value="Test push notification 💨")
body = st.text_input("Body", value="This is a test message from APNs Tester 🚀")
userInfo = st.text_input(
"userInfo",
value="any additional data",
help="see the [docs](https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification#Create-the-JSON-payload) for details",
)
# if st.form_submit_button("Send Push Notification"):
if st.button("Send Push Notification"):
payload = create_payload(title, subtitle, body, userInfo)
jwt_token = create_jwt_token(auth_key, team_id, key_id)
for device_token in device_tokens.split():
response = send_push_notification(
bundle_id, device_token, EndPt[endpoint], payload, jwt_token
)
if response.status_code == 200:
st.write(f"✅ Notification sent: {device_token}")
else:
st.write(f"⚠️ Failed: {device_token}")