Send Push Notifications With LINE Notify API And Python

ARON HACK 亞倫害的
2 min readAug 8, 2024

--

Recently, I have been developing my personal AI assistant with Flutter and LLM, but got stuck when trying to send push notifications to my iPhone. However, I found that sending push notifications to iOS devices requires an Apple Developer account to get Apple Push Notification service (APNs) certificate, which costs $99 per year. I don’t want to pay for it, so I tried to find a free way to send push notifications. LINE Notify is a good choice.

Step 1. Visit LINE Notify

Go to the LINE Notify page, scroll down, and find “add service”.

Step 2. Fill The Form

Two points need to be noticed. First, the service name is the name before the message. Pick a good name you like.

Second, the callback URL will be used later, but you don’t have to create a page or do anything on your server.

Step 3. Receive Verification Email

Step 4. Access The Service

Now you can access the service and get the client id and client secret.

Step 5. Visit The Link To Connect A Chat

Next, adjust the URL and enter the link. This step corresponds to the screenshot of the document. By the way, according to the document, the state is used to respond to CSRF attacks. It doesn’t show in the notification.

https://notify-bot.line.me/oauth/authorize?response_type=code&client_id=client_id&redirect_uri=your_callback_url&scope=notify&state=your_custom_state

Step 6. Select A Chat

Step 7. Copy The Code In URL

In the old tutorials, a popup will show the access_token after clicking “Agree and connect”. However, the process is changed. You will be redirected to the URL below, copy the code.

your_callback_url?code=generated_code&state=your_state

Step 8. Get Access_Token

def get_token(redirect_uri, client_id, secret, code):
url = 'https://notify-bot.line.me/oauth/token'
payload =
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': secret

r = requests.post(url, params=payload)
return r.text

If success, you will get a response like:

\n  "status" : 200,\n  "message" : "access_token is issued",\n  "access_token" : your_access_token\n

Step 9. Invite LINE Notify To The Chat

To send notifications to the chat, It is essential to invite LINE Notify.

Step 10. Start To Send Notifications!

def send_line_notify(message, access_token):
url = 'https://notify-api.line.me/api/notify'
headers =
'Authorization': f'Bearer access_token'

payload = 'message': message

response = requests.post(url, headers=headers, params=payload)
return response

You can also find the code on my GitHub.

https://aronhack.com/send-push-notifications-with-line-notify-api-and-python/?feed_id=98&_unique_id=66b4f31b5a51e

--

--

ARON HACK 亞倫害的
ARON HACK 亞倫害的

Written by ARON HACK 亞倫害的

唸過設計,當過行銷企劃,蓋過電商網站的零售業數據分析師。 https://www.aronhack.com/ | https://www.linkedin.com/in/aronwu/

No responses yet