LocalNotification v6 Not Working

I’m using Ionic v8 and Capacitor LocalNotification v6.
I’m compiling it for android.

I’ve the following permissions set in my AndroidManifest.xml file

 <uses-permission
    android:name="android.permission.SCHEDULE_EXACT_ALARM"
    android:maxSdkVersion="32"
  />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.USE_EXACT_ALARM" />

The push notification doesn’t work in production. The app is on play store.

I’ve this:

 const scheduleNotification = async () => {
    await store.create();
    const notification = await store.get("notification");
    const value ="1";
    const options: ScheduleOptions = {
      notifications: [
        {
          id: 0,
          title: "Don't forget your rewards!",
          body: "Time to earn",
          largeBody: "Time to earn some free XAH",
          summaryText: "Earn XAH",
          largeIcon: "res://drawable/icon_bel48",
          smallIcon: "res://drawable/icon_bel48",
          sound: "notify.wav",
          schedule: { every: "hour", count: 8, allowWhileIdle: true },
        },
      ],
    };
    const status: PermissionStatus =
      await LocalNotifications.checkPermissions();
    if (status.display === "granted" && notification !== value) {
      await LocalNotifications.schedule(options);
      await store.set("notification", value);
    } else {
      await LocalNotifications.requestPermissions();
      await store.set("notification", "revoke");
    }
  };

capacitor.config.json

{
“appId”: “com.webphone.app”,
“appName”: “webphone”,
“webDir”: “build”,
“bundledWebRuntime”: false,
“plugins”: {
“LocalNotifications”: {
“smallIcon”: “ic_stat_icon”
},
“BackgroundRunner”: {
“enabled”: true
}
}
}

import React, { useEffect } from ‘react’;
import { BackgroundRunner } from ‘@capacitor/background-runner’;
import { LocalNotifications } from ‘@capacitor/local-notifications’;
import { Capacitor } from ‘@capacitor/core’;

const BG = () => {
useEffect(() => {
const registerBackgroundTask = async () => {
try {
await BackgroundRunner.register({
taskId: ‘background-task’,
task: async () => {
console.log(‘Background task is running’);

        // Schedule a local notification from the background task
        await LocalNotifications.schedule({
          notifications: [
            {
              title: 'Background Task Notification',
              body: 'Your background task has run.',
              id: 1,
              schedule: { at: new Date(Date.now() + 1000) }, // 1 second delay
              sound: null,
              attachments: null,
              actionTypeId: '',
              extra: null,
            },
          ],
        });
      },
    });
  } catch (error) {
    console.error('Error registering background task:', error);
  }
};

registerBackgroundTask();

const showTestNotification = async () => {
  await LocalNotifications.schedule({
    notifications: [
      {
        title: 'Test Notification',
        body: 'Your app is running in the background.',
        id: 2,
        schedule: { at: new Date(Date.now() + 10000) }, // 10 seconds delay
        sound: null,
        attachments: null,
        actionTypeId: '',
        extra: null,
      },
    ],
  });
};

showTestNotification();

const handleNotificationAction = (notification) => {
  if (Capacitor.isNativePlatform()) {
    alert('Notification Clicked: ' + notification.notification.title);
    // Handle app navigation or actions here
  }
};

LocalNotifications.addListener('localNotificationActionPerformed', handleNotificationAction);

// Cleanup function to remove the listener
return () => {
  LocalNotifications.removeListener('localNotificationActionPerformed', handleNotificationAction);
};

}, );

return (


Capacitor Background Runner Test


Wait for 10 seconds to see the test notification.



);
};

export default BG;

Test notifications do appear for me. But they don’t in production. My app is in play store and I never get any local notification. :frowning:

Have you tried with just

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

with the USE_EXACT_ALARM permission removed?

Also, maybe try schedule.on, something like

schedule: { on: { hour: 12 }, allowWhileIdle: true }

I am just using SCHEDULE_EXACT_ALARM and schedule.on with Capacitor 6 with success. Just trying to help you narrow down where the issue may be.

Also, why not check permissions and request permissions if need be before anything else is done? Right now, if permissions are not granted, nothing gets scheduled after request is called and granted.

What is notification !== value for?

@twestrick

I had tried with only this permission and it didn’t work

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

But I had every: '8'

So I’ll try your suggestion now and see if it works.

About notification !== value
^ this is something I’m confused about. If the notification is set once, it’ll keep executing after every 8 hours. That’s what I think. That’s why I’m not setting the notification every time the user uses my app. Using notification !== value, I’m only setting the notification once. Is it wrong?

Should I make the code like this:

const scheduleNotification = async () => {

    const options: ScheduleOptions = {
      notifications: [
        {
          id: 0,
          title: "Don't forget your rewards!",
          body: "Time to earn",
          largeBody: "Time to earn some free XAH",
          summaryText: "Earn XAH",
          largeIcon: "res://drawable/icon_bel48",
          smallIcon: "res://drawable/icon_bel48",
          sound: "notify.wav",
          schedule: { on: { hour: 8 }, allowWhileIdle: true },
        },
      ],
    };
    const status: PermissionStatus =
      await LocalNotifications.checkPermissions();
    if (status.display === "granted" ) {
      await LocalNotifications.schedule(options);
    } else {
      await LocalNotifications.requestPermissions();
    }

  };

And should I call scheduleNotification() every time the user uses my app??

I want to get notification every 8 hours.