APEX Integration with Gmail Notifications
Introduction
In many Oracle APEX applications, users want to see Gmail notifications directly within the application without opening Gmail.
In this article, we will build a Gmail notification badge that displays the number of unread emails in the APEX Navigation Bar.
Final Output
- Notification bell in the Navigation Bar
- Displays unread Gmail count
- Automatically refreshes every 60 seconds
- Works across the entire APEX application using the Global Page (Page 0)
Architecture
Gmail
↓
Google Apps Script
↓
REST Web App
↓
Oracle APEX (Page 0)
↓
Navigation Bar Badge
Prerequisites
- Oracle APEX (Works on apex.oracle.com)
- Personal Gmail account
- Google Apps Script
- Internet access
1Create a Google Apps Script Project
- Open https://script.google.com/
- Click New Project.
- Rename the project.
Ex: APEX Gmail Notification
2Write the Google Apps Script
Replace the default code with the following:
function doGet(e) {
if (!e || !e.parameter || e.parameter.key !== "MySecret123") {
return ContentService
.createTextOutput("Unauthorized")
.setMimeType(ContentService.MimeType.TEXT);
}
var threads = GmailApp.search("is:unread", 0, 500);
return ContentService
.createTextOutput(
JSON.stringify({
unreadCount: threads.length
})
)
.setMimeType(ContentService.MimeType.JSON);
}
Code Explanation
- Checks the security key passed in the URL.
- Searches for unread Gmail conversations.
- Returns the unread count as JSON.
3Deploy the Script
Deploy > New Deployment > Step 3: Deploy the Script
| Property | Value |
|---|---|
| Execute As | Me |
| Who has access | Anyone |
4Authorize the Script
- Choose your Gmail account.
- Advanced > Go to <Project Name> (unsafe) > Allow
5Copy the Web App URL
https://script.google.com/macros/s/AKfycbxxxxxxxxxxxxxxxxxxxx/exec
6Test the URL
Append the secret key:
https://script.google.com/macros/s/AKfycbxxxxxxxxxxxxxxxxxxxx/exec?key=MySecretKey
Output:
{
"unreadCount":5
}
"unreadCount":5
}
7Create static content on Global Page in Oracle APEX
- Slot: After Logo
- Template: Blank with attributes
8Paste the following HTML to the region
<div id="gmailNotification" class="gmail-notification">
<span class="fa fa-bell"></span>
<span id="gmailBadge" class="gmail-badge" style="display:none;"></span>
</div>
9Using below JS code under <Script> tag
function loadGmailCount(){
fetch("YOUR_SCRIPT_URL?key=MySecret123")
.then(response => response.json())
.then(data => {
var count = data.unreadCount || 0;
if(count > 0){
$("#gmailBadge")
.text(count)
.show();
}else{
$("#gmailBadge").hide();
}
})
.catch(function(err){
console.log(err);
});
}
loadGmailCount();
setInterval(loadGmailCount,60000);
Comments
Post a Comment