Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class NotifServices {
- // OneSignal Credentials and API endpoint.
- static const String _appId = "app-id-nako";
- static const String _authKey = "Basic auth-key-nako";
- static const String _onesignalUrl = "https://onesignal.com/api/v1/notifications";
- static Future<void> sendGroupNotification({
- required String userType,
- required String heading,
- required String content,
- String? bigPicture,
- }) async {
- final Map<String, dynamic> notificationData = {
- "app_id": _appId,
- "filters": [
- {
- "field": "tag",
- "key": "userType",
- "relation": "=",
- "value": userType,
- }
- ],
- "headings": {"en": heading},
- "contents": {"en": content},
- };
- if (bigPicture != null) {
- notificationData["big_picture"] = bigPicture;
- }
- try {
- final response = await http.post(
- Uri.parse(_onesignalUrl),
- headers: {
- "Content-Type": "application/json; charset=utf-8",
- "Authorization": _authKey,
- },
- body: jsonEncode(notificationData),
- );
- if (response.statusCode == 200) {
- print("Group notification sent successfully: ${response.body}");
- } else {
- print("Failed to send group notification: ${response.body}");
- }
- } catch (e) {
- print("============================: $userType, $heading, $content");
- print("Exception sending group notification: $e");
- }
- }
- static Future<void> sendIndividualNotification({
- required String playerId,
- required String heading,
- required String content,
- String? bigPicture,
- }) async {
- final Map<String, dynamic> notificationData = {
- "app_id": _appId,
- "include_player_ids": [playerId],
- "headings": {"en": heading},
- "contents": {"en": content},
- };
- if (bigPicture != null) {
- notificationData["big_picture"] = bigPicture;
- }
- try {
- final response = await http.post(
- Uri.parse(_onesignalUrl),
- headers: {
- "Content-Type": "application/json; charset=utf-8",
- "Authorization": _authKey,
- },
- body: jsonEncode(notificationData),
- );
- if (response.statusCode == 200) {
- print("Individual notification sent successfully: ${response.body}");
- } else {
- print("Failed to send individual notification: ${response.body}");
- }
- } catch (e) {
- print("Exception sending individual notification: $e");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement