Can anyone that has a working android function that checks AND schedule notifications correctly please share? my functions for the love of me seem to work but i get hit in the logcat with “line 351 - undefined” ??? it’s like the capacitor gods themselves just decided to not make my android users get notifications today even though it used to work back then i’m really lost
async function createAndroidChannel() {
try {
await LocalNotifications.createChannel({
id: 'messages',
name: 'Message notifications',
importance: 3,
visibility: 1,
sound: 'default',
vibration: true,
});
} catch (error) {
console.error('Failed to create notification channel:', error);
}
}
async function checkAndRequestPermissions() {
if (isAndroid) {
const permStatus = await LocalNotifications.checkPermissions();
if (permStatus.display === 'granted') {
await createAndroidChannel();
console.log('Notification permission granted - check');
return true;
} else {
const perm = await LocalNotifications.requestPermissions();
if (perm.display === 'granted') {
console.log('Notification permission granted - request');
return true;
}
console.log('Notification permission denied');
return false;
}
} else {
const perm = await LocalNotifications.requestPermissions();
if (perm.display === 'granted') {
console.log('Notification permission granted');
return true;
}
console.log('Notification permission denied');
return false;
}
}
async function checkUnreadMessagesAndNotify() {
try {
const userKey = await Preferences.get({ key: 'user' });
if (userKey.value) {
const secretKey = JSON.parse(userKey.value).secret_key;
try {
const response = await axios.get(`${base_url}/api/unread-message-details/`, {
headers: {
Authorization: `Bearer ${secretKey}`
}
});
response.data.forEach(async (msg) => {
let messageContent = msg.last_message;
if (messageContent.startsWith("/api/files/FLASK_IMAGES")) {
messageContent = "🖼️ Image";
}
if (!msg.seen && !notifiedMessageContents.value.has(messageContent)) {
const uniqueNotifId = isAndroid ? Math.floor(Math.random() * (2147483647 - 1)) + 1 : Math.floor((new Date().getTime() + Math.random() * 1000));
const containsLink = /https?:\/\/[^\s]+/g.test(messageContent);
try {
await LocalNotifications.schedule({
notifications: [
{
title: `New message from ${msg.sender_username}`,
body: messageContent,
id: uniqueNotifId,
schedule: { at: new Date(Date.now() + 1000), allowWhileIdle: true },
extra: { chatId: msg.chat_id, link: messageContent },
actionTypeId: containsLink ? 'REPLY_WITH_LINK' : 'REPLY',
//channelId: isAndroid ? 'messages' : null,
}
]
});
console.log(`Notification scheduled with ID: ${uniqueNotifId}`);
notifiedMessageContents.value.add(messageContent);
} catch (notificationError) {
console.error('Error scheduling notification:', notificationError);
}
}
});
} catch (axiosError) {
console.error('Error fetching unread messages:', axiosError);
}
}
} catch (preferencesError) {
console.error('Error retrieving user preferences:', preferencesError);
}
}
const testAlarm = async () => {
if (isAndroid) {
try {
const exactAlarmStatus = await LocalNotifications.checkExactNotificationSetting();
if (exactAlarmStatus.exact_alarm !== 'granted') {
console.warn('Exact alarm permission is not granted. Notifications may not be exact.');
try {
const changeStatus = await LocalNotifications.changeExactNotificationSetting();
if (changeStatus.exact_alarm !== 'granted') {
console.warn('User did not grant exact alarm permission.');
} else {
console.log('Exact alarm permission granted.');
}
} catch (changeError) {
console.error('Error changing exact alarm setting:', changeError);
}
} else {
console.log('Exact alarm permission is already granted.');
}
} catch (error) {
console.error('Error checking exact alarm setting:', error);
}
}
}
Can anyone familiar enough with android and ionic tell me where did i go wrong? i must also add it works as expected on ios but not android for some reason any help is appreciated