Skip to content

Instantly share code, notes, and snippets.

@liamcottle
Created August 15, 2021 17:23
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save liamcottle/86180844b81fcf8085e2c9182daa278c to your computer and use it in GitHub Desktop.
A NodeJS script for sending custom notifications to Plex Users in the mobile app.
const axios = require('axios');
/**
* PLEX_TOKEN: Your X-Plex-Token
* PLEX_IDENTIFIER: Your Plex Server Identifier
* PLEX_SERVER_NAME: Your Plex Server Name
*/
const PLEX_TOKEN = '';
const PLEX_IDENTIFIER = '';
const PLEX_SERVER_NAME = '';
/**
* Notification will be formatted like so:
* - title: "New Movie on <PLEX_SERVER_NAME>"
* - message: "<NOTIFICATION_MESSAGE>"
*
* When the notification is clicked, the Plex app will load <NOTIFICATION_URL>.
* You can set it to null if you don't want to load a website. However, Plex will still launch.
*/
const NOTIFICATION_MESSAGE = "My Plex Server will be offline from 2:30pm - 3:00pm.";
const NOTIFICATION_URL = "https://github.com/liamcottle";
/**
* A list of Plex User Ids in integer or string format, that you want to send the notification to.
* You could specify a list of specific users, or fetch all user ids from the Plex API.
* User Ids can be found here: https://plex.tv/api/users/?X-Plex-Token=<PLEX_TOKEN>
*/
const USERS_TO_NOTIFY = [
"<user id>",
];
/**
* Send a notification to the Plex Users.
*
* This requires the user to have their "New Content Added to Library"
* notification preference set to "On" for the Plex Server associated with
* the provided <PLEX_IDENTIFIER>
*
* The other notification identifiers don't really work well due to how
* the Plex API formats them.
*/
return axios.post('https://notifications.plex.tv/api/v1/notifications', {
group: 'media',
identifier: 'tv.plex.notification.library.new',
to: USERS_TO_NOTIFY,
play: false,
data: {
provider: {
identifier: PLEX_IDENTIFIER,
title: PLEX_SERVER_NAME,
},
},
metadata: {
type: 'movie',
title: NOTIFICATION_MESSAGE,
},
uri: NOTIFICATION_URL,
}, {
headers: {
'X-Plex-Token': PLEX_TOKEN,
},
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment