skip to Main Content

I am making a Calendar app where the user can schedule events.

The problem: User creates an event that repeats every week/day/month/year. I need to schedule a notification for every week/day/month/year at that date forever using the OneSignal API

What I’ve done

✅ Created normal scheduled notifications with POST requests and API

✅ Found a similar method using another plugin:

await flutterLocalNotificationsPlugin.showDailyAtTime(2, 'notification title', 'message here',
time, platformChannel,payload: 'new payload'));

I would need to translate this but using OneSignal.

What do I need?

  • Find an option in OneSignal API to setup a recurring scheduled notification [Should be possible]
  • In the case this is not possible, some logic or guidance as to how to schedule infinite possible notifications.

3

Answers


  1. I have faced this issue, and I couldn’t find any way to schedule notifications via OneSignal API.

    What I did was I created an express API and added an endpoint to create a cron job to schedule the notification sending via one signal REST API.

    Login or Signup to reply.
  2. I think there is no direct solution,but yeah you can able to get this functionality by updating your code like this,

    import 'package:onesignal_flutter/onesignal_flutter.dart';
    
    void scheduleRecurringNotifications(DateTime notificationTime, String content, String heading) async {
      // Schedule notifications for next year
      for (int i = 0; i < 52; i++) {
        // Calculate  next notification time
        DateTime nextNotificationTime = notificationTime.add(Duration(days: i * 7));
    
        OSCreateNotification notification = OSCreateNotification(
          appId: "YOUR_APP_ID",
          content: content,
          heading: heading,
          sendAfter: nextNotificationTime,
        );
    
        // Send notification to OneSignal API
        await OneSignal.shared.postNotification(notification);
      }
    }
    

    also for more info, you can find it out here this link describe other functionality as well

    edited 1:- one more suggestion is you can use "delayed_option" and "daily" for Recurring notifications according to your situation

    I found this in here

    Map<String, dynamic> notification = {
      "app_id": "YOUR_APP_ID",
      "contents": {"en": "This is a recurring notification"},
      "headings": {"en": "Recurring Notification"},
      "send_after": "2023-03-16 12:00:00 GMT-0400",
      "delayed_option": "daily",
      "delivery_time_of_day": "12:00:00 GMT-0400",
    };
    
    final headers = {
      'Content-Type': 'application/json',
      'Authorization': 'YOUR_REST_API_KEY',
    };
    
    final response = await http.post(
      Uri.parse("https://onesignal.com/api/v1/notifications"),
      headers: headers,
      body: jsonEncode(notification),
    );
    
    Login or Signup to reply.
  3. Based on my own knowledge, there isn’t a direct solution to schedule recurring notifications using OneSignal API but you can make something:

    First, make sure you have all the dependencies:

    import 'package:onesignal_flutter/onesignal_flutter.dart';
    import 'package:http/http.dart' as http;
    import 'dart:convert';
    

    Then use a function like this to schedule daily notifications (make sure to update APIs)

    Future<void> scheduleDailyNotification(
        String title, String message, DateTime startTime) async {
      Map<String, dynamic> notification = {
        "app_id": "YOUR_APP_ID",
        "contents": {"en": message},
        "headings": {"en": title},
        "send_after": startTime.toUtc().toIso8601String(),
        "delayed_option": "daily",
        "delivery_time_of_day": startTime.toUtc().toIso8601String().substring(11, 19),
      };
    
      final headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Basic YOUR_REST_API_KEY',
      };
    
      final response = await http.post(
        Uri.parse("https://onesignal.com/api/v1/notifications"),
        headers: headers,
        body: jsonEncode(notification),
      );
    }

    Then call the function

    scheduleDailyNotification(
      'Daily Notification Title',
      'Daily Notification Message',
      DateTime.now().add(Duration(minutes: 1)),
    );

    This will schedule a daily notification starting one minute from the time of scheduling.

    This solution can be suitable for daily recurring notifications and you may need to use a similar approach for weekly/monthly/yearly notifications by modifying the delayed_option and delivery_time_of_day values in the request.

    For the scalability issue, I would create a backend service with an endpoint that creates a cron job to schedule the notification sending via OneSignal.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search