Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification not showing Android 15 #1154

Open
ihsa-gonzalo opened this issue Dec 6, 2024 · 5 comments
Open

Notification not showing Android 15 #1154

ihsa-gonzalo opened this issue Dec 6, 2024 · 5 comments

Comments

@ihsa-gonzalo
Copy link

ihsa-gonzalo commented Dec 6, 2024

I am using the notifee library for react native with this code

const createTriggerNotification = async () => {
    await checkApplicationPermission();

    const date = new Date(Date.now());

    date.setMinutes(date.getMinutes() + 1);

    console.log(date.getTime());

    await createNotification(date);
  };

export const checkApplicationPermission = async () => {
  if (Platform.OS === 'android') {
    try 
    {
      await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
      );
    } catch (error) {
      console.log(error)
    }
  }
};

const createTrigger  = (date : Date) =>
{
  const trigger: TimestampTrigger = {
    type: TriggerType.TIMESTAMP,
    timestamp: date.getTime(),
    alarmManager: {
      allowWhileIdle: true,
    },
  };

  return trigger
}

const createChannel = async () => {
  await notifee.createChannel({
  id: 'goc-channel',
  name: 'goc-channel',
  lights: false,
  vibration: true,
  badge: true,
  importance: AndroidImportance.HIGH,
})
};

export const createNotification = async (date : Date) => {
  console.log(date.toLocaleString())
  try
  {
    await createChannel();
    await notifee.createTriggerNotification(
      {
        title: 'Meeting with Jane',
        body: 'Today at 11:20am',
        android: {
          importance: AndroidImportance.HIGH,
          badgeIconType : AndroidBadgeIconType.SMALL,
          smallIcon: 'ic_notification',
          channelId: 'goc-channel',
          pressAction: {
            id: "default",
          },
        },
      },
      createTrigger(date),
    );
  }
  catch (e)
  {
      console.log(e)
  }
      

}

On devices with Android 13 and 14 the notification is It displays the correct time but with Android 15 it doesn't appear. It creates itself fine but nothing happens. Something I noticed is that on the first two devices the date appears like this

12/5/2024, 2:27:00 PM 6/12/2024, 10:23:09 AM

but with Android 15

6/12/2024, 01: 54:48

In all cases the timestamp is correct and verified

PD:

In Android 15 with this code, the notification is showing

 async function onDisplayNotification() {
    // Request permissions (required for iOS)
    await notifee.requestPermission()

// Create a channel (required for Android)
const channelId = await notifee.createChannel({
  id: 'default',
  name: 'Default Channel',
});

// Display a notification
await notifee.displayNotification({
  title: 'Notification Title',
  body: 'Main body content of the notification',
  android: {
    channelId,
    smallIcon: 'name-of-a-small-icon', // optional, defaults to 'ic_launcher'.
    // pressAction is needed if you want the notification to open the app when pressed
    pressAction: {
      id: 'default',
    },
  },
});
@mikehardy
Copy link
Collaborator

If you could post a full App.tsx that had a button to post a working notification and a button that failed on Android 15 that would really help - I could drop in a fresh reproducer app that would help. Something I can just drop in with no changes

@ihsa-gonzalo
Copy link
Author

If you could post a full App.tsx that had a button to post a working notification and a button that failed on Android 15 that would really help - I could drop in a fresh reproducer app that would help. Something I can just drop in with no changes

It is a complex app to just share that file.

The try-catch does not throw any errors. The notification is created well because when I show them, it appears. It must be something with the permissions because on two other phones it does appear programmed.

@mikehardy
Copy link
Collaborator

I did not ask for a complex app. I asked you for a minimal viable reproducer App.tsx. nothing but what is required to produce the problem you describe

https://stackoverflow.com/help/minimal-reproducible-example

I recommend using react-native-permissions for all permission stuff, regardless - you might try centralizing all your permission handling to that

@ihsa-gonzalo
Copy link
Author

When I enter the app information, the notification permission is enabled and even the channel (essential for new versions). For notifications that I create on demand, it appears normally. Not the scheduled ones.

@ihsa-gonzalo
Copy link
Author

ihsa-gonzalo commented Dec 6, 2024

I did not ask for a complex app. I asked you for a minimal viable reproducer App.tsx. nothing but what is required to produce the problem you describe

https://stackoverflow.com/help/minimal-reproducible-example

I recommend using react-native-permissions for all permission stuff, regardless - you might try centralizing all your permission handling to that

import React from 'react';

// UTILS
import {checkApplicationPermission, createNotification, } from '../../utils';

// REACT NATIVE
import {
  View,
  Button,
} from 'react-native';


import notifee from '@notifee/react-native';

export default function CheckListScreen() {

  async function onDisplayNotification() {
    // Request permissions (required for iOS)
    await checkApplicationPermission()

    // Create a channel (required for Android)
    const channelId = await notifee.createChannel({
      id: 'default',
      name: 'Default Channel',
    });

    // Display a notification
    await notifee.displayNotification({
      title: 'Notification Title',
      body: 'Main body content of the notification',
      android: {
        channelId,
        // pressAction is needed if you want the notification to open the app when pressed
        pressAction: {
          id: 'default',
        },
      },
    });
  }

  async function onDisplayDelayNotification() 
  {
    await checkApplicationPermission()
    createNotification();
  }

  return (
    <>
      <View>
      <Button title="Display Notification" onPress={() => onDisplayNotification()} />
      <Button title="Display Notification in two minutues" onPress={() => onDisplayDelayNotification()} />
    </View>
    </>
  );
}

utils

export const checkApplicationPermission = async () => {
  if (Platform.OS === 'android') {
    try 
    {
      await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
      );
    } catch (error) {
      console.log(error)
    }
  }
};

const createTrigger  = () =>
{
  const trigger: TimestampTrigger = {
    type: TriggerType.TIMESTAMP,
    timestamp: Date.now() + 1000 * 60 * 2, // fire in 2 minutes
    alarmManager: {
      type: AlarmType.SET_ALARM_CLOCK
    },
    
  };

  return trigger
}

const createChannel = async () => {
  await notifee.createChannel({
  id: 'goc-channel',
  name: 'goc-channel',
  lights: false,
  vibration: true,
  badge: true,
  importance: AndroidImportance.HIGH,
})
};

export const createNotification = async () => {
  try
  {
    await createChannel();
    await notifee.createTriggerNotification(
      {
        title: 'Meeting with Jane',
        body: 'Today at 11:20am',
        android: {
          importance: AndroidImportance.HIGH,
          badgeIconType : AndroidBadgeIconType.SMALL,
          smallIcon: 'ic_notification',
          channelId: 'goc-channel',
          pressAction: {
            id: "default",
          },
        },
      },
      createTrigger(),
    );
  }
  catch (e)
  {
      console.log(e)
  }
      

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants