Delete Indie Notifications
This guide covers how to delete notifications inside of an Indie Notification Inbox.
Delete Indie Notification Preview
Here's how the Delete Indie Notification process works:
- Get Notification ID: Each notification in your Indie Notification Inbox has a notification_id. You will need this notification_id to delete the notification.
- Use: Use the notification_id and the deleteIndieNotificationInbox function to delete the notification.
Prerequisites
Create a free Native Notify account
Create a free NativeNotify.com account to get your Native Notify App ID and App Token.
Create an Indie Notification Inbox
Create an Indie Notification Inbox by following the instructions on the Create Indie Inbox page.
Setup and Use
Import deleteIndieNotificationInbox
In your Indie Notification Inbox screen, include these imports:
import React, { useState, useEffect } from 'react';
import { deleteIndieNotificationInbox } from 'native-notify';
Create a delete function
Inside of your Notification Inbox screen, create a function with a notification_id parameter. Here is an example:
const handleDeleteNotification = async (notificationId) => {
try {
await deleteIndieNotificationInbox(
"unique-user-id",
notificationId,
app-id-number,
"app-token-string"
);
fetchNotifications(); // reload the list in the same shape as getIndieNotificationInbox
} catch (error) {
console.error("Error deleting notification: ", error);
}
}
The "unique-user-id" is the unique user ID you registered with Native Notify during the Indie Push Notification registration process.
Note:
- The deleteIndieNotificationInbox function resolves with the subscriber's 10 most recent remaining rows as raw database rows (
date_sent,push_data,notification_read, … — not thedate/pushData/readshape the list returns), so reload the list with getIndieNotificationInbox instead of rendering that result. - On an account whose free trial has ended, or on the Premium plan (the inbox is a Pro plan feature), the server answers
201with an explanation text and nothing is deleted. - A failed request (a wrong app token, a network error) rejects — keep the
try/catch. - The getIndieNotificationInbox guide can be found here: getIndieNotificationInbox Guide
Use the delete function
Attach the handleDeleteNotification function to mapped notifications inside of your Indie Notification Inbox so you have access to the notification IDs.
Example function use:
handleDeleteNotification(notification.notification_id)
Complete example
import React, { useState, useEffect } from 'react';
import { getIndieNotificationInbox, deleteIndieNotificationInbox } from 'native-notify';
export default function NotificationInbox() {
const [takeNumber, setTakeNumber] = useState(10);
const [skipNumber, setSkipNumber] = useState(0);
const [data, setData] = useState([]);
const fetchNotifications = async () => {
try {
const notifications = await getIndieNotificationInbox(
"unique-user-id",
"app-id-number",
"app-token-string",
takeNumber,
skipNumber
);
console.log("notifications: ", notifications);
setData(notifications);
} catch (error) {
console.error("Error fetching notifications: ", error);
}
};
useEffect(() => {
fetchNotifications();
}, []);
const handleDeleteNotification = async (notificationId) => {
try {
await deleteIndieNotificationInbox(
"unique-user-id",
notificationId,
app-id-number,
"app-token-string"
);
fetchNotifications(); // reload the list in the same shape as getIndieNotificationInbox
} catch (error) {
console.error("Error deleting notification: ", error);
}
}
return (
...
// attach your handleDeleteNotification to mapped notifications
// inside of your Indie Notification Inbox
// so you have access to the notification.notification_id
// example function use:
// handleDeleteNotification(notification.notification_id)
)
}