Overview
The Notification class represents a notification from Twitter/X, such as when someone likes your tweet, follows you, or mentions you.
Notification Class
Attributes
The unique identifier of the notification.
The timestamp of the notification in milliseconds.
Dictionary containing icon data for the notification. This represents the visual icon displayed with the notification (e.g., heart for likes, retweet symbol for retweets).
The message text of the notification.
The tweet associated with the notification (if applicable).
The user who triggered the notification.
Examples
Get and Display Notifications
# Get notifications
notifications = await client.get_notifications()
print("Recent Notifications:")
print("-" * 50)
for notification in notifications:
print(f"From: @{notification.from_user.screen_name}")
print(f"Message: {notification.message}")
if notification.tweet:
print(f"Related tweet: {notification.tweet.text[:50]}...")
# Convert timestamp to readable date
from datetime import datetime
timestamp = datetime.fromtimestamp(notification.timestamp_ms / 1000)
print(f"Time: {timestamp.strftime('%Y-%m-%d %H:%M:%S')}")
print()
Filter Notifications by Type
# Get notifications and filter by message content
notifications = await client.get_notifications()
# Separate notifications by type
likes = []
retweets = []
follows = []
mentions = []
for notif in notifications:
if 'liked' in notif.message.lower():
likes.append(notif)
elif 'retweeted' in notif.message.lower():
retweets.append(notif)
elif 'followed' in notif.message.lower():
follows.append(notif)
elif 'mentioned' in notif.message.lower():
mentions.append(notif)
print(f"Likes: {len(likes)}")
print(f"Retweets: {len(retweets)}")
print(f"New Followers: {len(follows)}")
print(f"Mentions: {len(mentions)}")
Respond to Notifications
# Get notifications and automatically respond
notifications = await client.get_notifications()
for notification in notifications:
# If someone mentioned you
if 'mentioned' in notification.message.lower() and notification.tweet:
tweet = notification.tweet
# Reply to the mention
await tweet.reply(f"Thanks for the mention @{notification.from_user.screen_name}!")
print(f"Replied to mention from @{notification.from_user.screen_name}")
# If someone followed you
elif 'followed' in notification.message.lower():
# Follow them back
await notification.from_user.follow()
print(f"Followed back @{notification.from_user.screen_name}")
Track Engagement
from collections import defaultdict
from datetime import datetime, timedelta
# Get notifications from the last 24 hours
notifications = await client.get_notifications()
engagement_stats = defaultdict(int)
one_day_ago = datetime.now() - timedelta(days=1)
for notification in notifications:
notif_time = datetime.fromtimestamp(notification.timestamp_ms / 1000)
if notif_time >= one_day_ago:
if 'liked' in notification.message.lower():
engagement_stats['likes'] += 1
elif 'retweeted' in notification.message.lower():
engagement_stats['retweets'] += 1
elif 'followed' in notification.message.lower():
engagement_stats['new_followers'] += 1
elif 'replied' in notification.message.lower():
engagement_stats['replies'] += 1
print("Engagement in the last 24 hours:")
for metric, count in engagement_stats.items():
print(f" {metric}: {count}")
from collections import Counter
# Get notifications and find which tweet got the most engagement
notifications = await client.get_notifications()
tweet_engagement = Counter()
for notification in notifications:
if notification.tweet:
tweet_engagement[notification.tweet.id] += 1
if tweet_engagement:
most_engaged_tweet_id = tweet_engagement.most_common(1)[0][0]
engagement_count = tweet_engagement.most_common(1)[0][1]
# Get the tweet details
tweet = await client.get_tweet_by_id(most_engaged_tweet_id)
print(f"Most engaged tweet ({engagement_count} notifications):")
print(f"Text: {tweet.text}")
print(f"Likes: {tweet.favorite_count}")
print(f"Retweets: {tweet.retweet_count}")
Notification Dashboard
from datetime import datetime
# Create a notification dashboard
notifications = await client.get_notifications()
print("═" * 60)
print(" NOTIFICATION DASHBOARD")
print("═" * 60)
print()
# Group by user
user_activity = {}
for notif in notifications:
user_handle = notif.from_user.screen_name
if user_handle not in user_activity:
user_activity[user_handle] = []
user_activity[user_handle].append(notif)
# Show top interactors
print("Top Interactors:")
print("-" * 60)
top_users = sorted(user_activity.items(), key=lambda x: len(x[1]), reverse=True)[:5]
for handle, notifs in top_users:
print(f"@{handle}: {len(notifs)} interactions")
for notif in notifs[:3]: # Show first 3
print(f" - {notif.message}")
if len(notifs) > 3:
print(f" ... and {len(notifs) - 3} more")
print()
Notifications help you stay informed about engagement with your tweets and account activity. You can use them to build automated responses, track engagement metrics, or create notification management tools.