定期通知 – OneSignal API Flutter

huangapple go评论97阅读模式
英文:

Recurring Notifications- OneSignal API Flutter

问题

我正在制作一个日历应用,用户可以安排事件。

问题: 用户创建一个每周/每天/每月/每年重复的事件。我需要使用OneSignal API在那个日期永远安排通知。

我已经完成的

✅ 使用POST请求和API创建了普通的定时通知

✅ 找到了使用另一个插件的类似方法:

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

我需要翻译这个,但是使用 OneSignal。

我需要什么?

  • 在 OneSignal API 中找到设置定期安排通知的选项[应该是可能的]
  • 如果这不可能,提供一些逻辑或指导,如何安排无限可能的通知。
英文:

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.

答案1

得分: 2

我遇到了这个问题,而我找不到通过 OneSignal API 安排通知的方法。

我所做的是创建了一个 Express API,并添加了一个端点来创建一个 cron job,以通过 OneSignal REST API 安排通知的发送。

英文:

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.

答案2

得分: 2

根据我的知识,使用OneSignal API没有直接的解决方案来安排定期通知,但你可以创建一些东西:

首先,确保你有所有的依赖项:

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

然后使用类似这样的函数来安排每日通知(确保更新API):

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),
  );
}

然后调用这个函数:

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

这将安排每日通知,从调度时间的一分钟后开始。

这个解决方案适用于每日重复通知,对于每周/每月/每年通知,你可能需要使用类似的方法修改请求中的delayed_option和delivery_time_of_day的值。

对于可扩展性问题,我会创建一个带有端点的后端服务,该端点创建一个cron作业,通过OneSignal安排通知发送。

英文:

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 &#39;package:onesignal_flutter/onesignal_flutter.dart&#39;;
import &#39;package:http/http.dart&#39; as http;
import &#39;dart:convert&#39;;

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Future&lt;void&gt; scheduleDailyNotification(
    String title, String message, DateTime startTime) async {
  Map&lt;String, dynamic&gt; notification = {
    &quot;app_id&quot;: &quot;YOUR_APP_ID&quot;,
    &quot;contents&quot;: {&quot;en&quot;: message},
    &quot;headings&quot;: {&quot;en&quot;: title},
    &quot;send_after&quot;: startTime.toUtc().toIso8601String(),
    &quot;delayed_option&quot;: &quot;daily&quot;,
    &quot;delivery_time_of_day&quot;: startTime.toUtc().toIso8601String().substring(11, 19),
  };

  final headers = {
    &#39;Content-Type&#39;: &#39;application/json&#39;,
    &#39;Authorization&#39;: &#39;Basic YOUR_REST_API_KEY&#39;,
  };

  final response = await http.post(
    Uri.parse(&quot;https://onesignal.com/api/v1/notifications&quot;),
    headers: headers,
    body: jsonEncode(notification),
  );
}

<!-- end snippet -->

Then call the function

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

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.

huangapple
  • 本文由 发表于 2023年3月7日 21:04:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定