how to use "export interface CalendarNotificationTrigger"

In the docs here https://docs.expo.io/versions/v38.0.0/sdk/notifications/#calendarnotificationtrigger

I see:

export interface CalendarNotificationTrigger {  ... }

I didn’t see anywhere on this page on how to use this interface. To be honest, I’ve never seen

export interface

and I am not sure how to integrate or call it in my code.

Please let me know where I can find a full example of this integration.

My goal is to use this as a trigger in the scheduleNotificationAsync method as seen here:
https://docs.expo.io/versions/v38.0.0/sdk/notifications/

Thank you,

Hey @dj-designs,

What you’re looking at is to be used with TypeScript. Interfaces are used for typing which is why the section of the docs you linked to is titled “Types”. If you’re not using TS, you’ll not need to use this.

Cheers,
Adam

thanks for the quick reply @adamjnav

I was wondering if you could help me? I’ve been struggling with notifications for about three months now.

I really appreciate expo and I really just appreciate all the hard work from the expo team and how it makes it so easy to publish to the App Store. I really love it! I have been able to publish my first mobile app Using expo and this is been one of my goals for the past seven years.

I’ve looked at other mobile application solutions but I think expo is the most robust and makes it easiest to publish to the App Store.

At any rate I know you were very busy but I was wondering if you could help me to figure out how to do a simple local notification based on the date.

I’ve been looking at the API here: https://docs.expo.io/versions/v38.0.0/sdk/notifications/#schedulenotificationasyncnotificationrequest-notificationrequestinput-promisestring

and I can get the notification appear when I click button (based on the sample code on this page). but I can’t figure out how to add this CalendarNotificationTrigger to the trigger of the scheduleNotificationAsync

Also, after looking at typescript more I was able to get this to write to the console using something like this:

const NewCal = (makeCal: CalendarNotificationTrigger) => {
  const month = new Date().getMonth();
  const dayOfMonth = new Date().getDate();

  if (month == makeCal.dateComponents.month) {
    console.log("it is ok")
  } else {
    console.log('no it is not')
  }
}

const trigger = {
  type: 'calendar',
  repeats: false,
  dateComponents: {
    hour: 5,
    month: 7,
    minute: 37,
  }
}

NewCal(trigger);

Notifications.scheduleNotificationAsync({
  content: {
    title: "calendar trigger!",
    body: 'Change sides!',
  },
  
  trigger,
      
});

based on the docs on the API page I should be able to add the variable trigger to the scheduleNotificationAsync method — as seen in this example in the same API docs (where trigger is inserted as the value of the trigger variable):

import * as Notifications from 'expo-notifications';

const trigger = new Date(Date.now() + 60 * 60 * 1000);
trigger.setMinutes(0);
trigger.setSeconds(0);

Notifications.scheduleNotificationAsync({
  content: {
    title: 'Happy new hour!',
  },
  trigger,
});

But I get an error when I attempt to add my trigger object as the value of the trigger key in the scheduleNotificationAsync method.

my goal is to create a local notification based on the current date.

Thank you,

:face_with_head_bandage:

hum…something very strange happened today. I got 45 notifications. I can tell they are from a few days ago because I change the text of the notification every day that I work on this. Why would that happened?

:sweat:

do I need to request permission to send local notifications? Local notifications appeared to be working (based on calendar trigger seen here: https://docs.expo.io/versions/latest/sdk/notifications/#calendarnotificationtrigger) on expo. But when I published my app to test flight I noticed that my app is not listed under “Notifications” in Settings

this means my app has not requested the user to send notifications. Apparently I need this (as seen here: https://docs.expo.io/versions/latest/sdk/notifications/#calendarnotificationtrigger):

async function registerForPushNotificationsAsync() {
  let token;
  if (Constants.isDevice) {
    const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let finalStatus = existingStatus;
    if (existingStatus !== 'granted') {
      const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status;
    }
    if (finalStatus !== 'granted') {
      alert('Failed to get push token for push notification!');
      return;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
    console.log(token);
  } else {
    alert('Must use physical device for Push Notifications');
  }

  if (Platform.OS === 'android') {
    Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }

in order to send local notifications. I guess it was working on the expo app because the expo app already asked for permission to send notifications?

I am willing to pay someone to help me figure this out.

hey there @notbrent @charliecruzan @wodin just wanted to reach out to see if ya’ll could help? I’ve been stuck on this for 3 months. I want to be fair and not just “ask for someone else to write my code.” I’m really just struggling with this.

I just made a recent push to my github repo: https://github.com/dominick-designs/react-native-app-ekadasi

I guess I am supposed to put the “request for permissions” function in componentDidMount in the root of the app?

The confusing part is that I am getting local notifications on my app during development on the expo app. But when I build and publish and test in test flight for iOS it does not prompt to allow permissions and when I look in Settings > Notifications on my iOS device I don’t see my test flight app.

@dj-designs sure you can request permissions in componentDidMount, or really anywhere else (as long as you’re requesting before you try to send any important notifications)

Are you saying that when you request permissions in your iOS app with await Permissions.askAsync(Permissions.NOTIFICATIONS), nothing happens?

If you’ve already granted the Expo app permissions to send notifications, you don’t need to ask again, so that would explain why it’s working in the Expo app. I recommended reading our push notifications guide end-to-end, it’s a huge help when implementing notifications into your app

@charliecruzan Thank you for the quick reply! Yes, I’ve been reading this push notification documentation for the past few months; after experimenting I concluded that I don’t need all of the push notification code if I am only doing local notifications. Maybe I am wrong?

Yes, that is correct. I have this snippet starting with registerForPushNotificationsAsync from the documentation you cited but nothing happens after I build and send to test flight on ios.

When I install my app (after building the ios version and sending to Test Flight and installing via test flight on my device) then I don’t see a request to “Allow” notifications. Even though during development I am seeing local notifications (only when app is in foreground) based on current date.

I just pushed an update on the Apple Store and when I updated my expo app on my personal ios device it prompted me “do you want this app to send notifications?” So, this is a good sign. I will follow up tomorrow because the way my app is coded I should receive a notification at 3 am tomorrow morning. Hoping for the best! :crossed_fingers:t2:

unfortunately the app did not display any notification.
good sign that at least it asked for permission to display notifications. I think that’s progress…

Has anyone had any luck with local notifications? Calendar-based notifications?

When you ask for permissions, does it return granted? I know that local notifications work, I use them myself and we have the example on the docs page using it. Happy to try and help you figure this out but I’ll need a minimal reproducible demo of your issue so I can help debug :smile: