Skip to content

Commit e384325

Browse files
committed
Fix Button Parsing
• Fixes how OSCreateNotification was parsing buttons to JSON (it was using a map which wasn't correctly parsing to an array of JSON buttons) • Fixes tabbing on OSCreateNotification • Fixes OSNotificationOpenedResult JSON parsing constructor, which assumed 'action' would always exist (it doesn't)
1 parent f5fc3e2 commit e384325

File tree

3 files changed

+79
-69
lines changed

3 files changed

+79
-69
lines changed

lib/src/create_notification.dart

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,90 +8,90 @@ import 'package:OneSignalFlutter/onesignal.dart';
88
/// a Create Notification object as an entirely different class
99
class OSCreateNotification extends JSONStringRepresentable {
1010

11-
/// An array of user ID's that should receive this notification
12-
List<String> playerIds;
11+
/// An array of user ID's that should receive this notification
12+
List<String> playerIds;
1313

14-
/// The notification's content (excluding title)
15-
String content;
14+
/// The notification's content (excluding title)
15+
String content;
1616

17-
/// The language code (ie. "en" for English) for this notification
18-
/// defaults to "en" (English)
19-
String languageCode;
17+
/// The language code (ie. "en" for English) for this notification
18+
/// defaults to "en" (English)
19+
String languageCode;
2020

21-
/// The title/heading for the notification
22-
String heading;
21+
/// The title/heading for the notification
22+
String heading;
2323

24-
/// The subtitle for the notification (iOS 10+ only)
25-
String subtitle;
24+
/// The subtitle for the notification (iOS 10+ only)
25+
String subtitle;
2626

27-
/// Tells the app to launch in the background (iOS only)
28-
bool contentAvailable;
27+
/// Tells the app to launch in the background (iOS only)
28+
bool contentAvailable;
2929

30-
/// Tells the app to launch the Notification Service extension,
31-
/// which can mutate your notification (ie. download attachments)
32-
bool mutableContent;
30+
/// Tells the app to launch the Notification Service extension,
31+
/// which can mutate your notification (ie. download attachments)
32+
bool mutableContent;
3333

34-
/// Additional data you wish to send with the notification
35-
Map<String, dynamic> additionalData;
34+
/// Additional data you wish to send with the notification
35+
Map<String, dynamic> additionalData;
3636

37-
/// The URL to open when the user taps the notification
38-
String url;
37+
/// The URL to open when the user taps the notification
38+
String url;
3939

40-
/// Media (images, videos, etc.) for iOS
41-
/// Maps a custom ID to a resource URL
42-
/// in the format {'id' : 'https://.....'}
43-
Map<String, String> iosAttachments;
40+
/// Media (images, videos, etc.) for iOS
41+
/// Maps a custom ID to a resource URL
42+
/// in the format {'id' : 'https://.....'}
43+
Map<String, String> iosAttachments;
4444

45-
/// An image to use as the big picture (android only)
46-
String bigPicture;
45+
/// An image to use as the big picture (android only)
46+
String bigPicture;
4747

48-
/// A list of buttons to attach to the notification
49-
List<OSActionButton> buttons;
48+
/// A list of buttons to attach to the notification
49+
List<OSActionButton> buttons;
5050

51-
/// The category identifier for iOS (controls various aspects
52-
/// of the notification, for example, whether to launch a
53-
/// Notification Content Extension) (iOS only)
54-
String iosCategory;
51+
/// The category identifier for iOS (controls various aspects
52+
/// of the notification, for example, whether to launch a
53+
/// Notification Content Extension) (iOS only)
54+
String iosCategory;
5555

56-
/// The sound to play (iOS only)
57-
String iosSound;
56+
/// The sound to play (iOS only)
57+
String iosSound;
5858

59-
/// The sound to play (Android only)
60-
String androidSound;
59+
/// The sound to play (Android only)
60+
String androidSound;
6161

62-
/// A small icon (Android only)
63-
/// Can be a drawable resource name or a URL
64-
String androidSmallIcon;
62+
/// A small icon (Android only)
63+
/// Can be a drawable resource name or a URL
64+
String androidSmallIcon;
6565

66-
/// A large icon (android only)
67-
/// Can be a drawable resource name or a URL
68-
String androidLargeIcon;
66+
/// A large icon (android only)
67+
/// Can be a drawable resource name or a URL
68+
String androidLargeIcon;
6969

70-
/// The Android Oreo Notification Category to send the notification under
71-
String androidChannelId;
70+
/// The Android Oreo Notification Category to send the notification under
71+
String androidChannelId;
7272

73-
/// can be 'Increase' or 'SetTo'
74-
OSCreateNotificationBadgeType iosBadgeType;
73+
/// can be 'Increase' or 'SetTo'
74+
OSCreateNotificationBadgeType iosBadgeType;
7575

76-
/// The actual badge count to either set to directly, or increment by
77-
/// To decrement the user's badge count, send a negative value
78-
int iosBadgeCount;
76+
/// The actual badge count to either set to directly, or increment by
77+
/// To decrement the user's badge count, send a negative value
78+
int iosBadgeCount;
7979

80-
/// If multiple notifications have the same collapse ID, only the most
81-
/// recent notification will be shown
82-
String collapseId;
80+
/// If multiple notifications have the same collapse ID, only the most
81+
/// recent notification will be shown
82+
String collapseId;
8383

84-
/// Allows you to send a notification at a specific date
85-
DateTime sendAfter;
84+
/// Allows you to send a notification at a specific date
85+
DateTime sendAfter;
8686

87-
/// You can use several options to send notifications at specific times
88-
/// ie. you can send notifications to different user's at the same time
89-
/// in each timezone with the 'timezone' delayedOption
90-
OSCreateNotificationDelayOption delayedOption;
87+
/// You can use several options to send notifications at specific times
88+
/// ie. you can send notifications to different user's at the same time
89+
/// in each timezone with the 'timezone' delayedOption
90+
OSCreateNotificationDelayOption delayedOption;
9191

92-
/// Used with delayedOption == timezone, lets you specify what time of day
93-
/// each user should receive the notification, ie. "9:00 AM"
94-
String deliveryTimeOfDay;
92+
/// Used with delayedOption == timezone, lets you specify what time of day
93+
/// each user should receive the notification, ie. "9:00 AM"
94+
String deliveryTimeOfDay;
9595

9696
OSCreateNotification({
9797
@required this.playerIds,
@@ -140,7 +140,7 @@ class OSCreateNotification extends JSONStringRepresentable {
140140
// add optional parameters to payload if present
141141
if (this.content != null) json['contents'] = { this.languageCode : this.content };
142142
if (this.heading != null) json['headings'] = { this.languageCode : this.heading };
143-
if (this.subtitle != null) json['subtitle'] = { this.languageCode : this.heading };
143+
if (this.subtitle != null) json['subtitle'] = { this.languageCode : this.subtitle };
144144
if (this.contentAvailable != null) json['content_available'] = this.contentAvailable;
145145
if (this.mutableContent != null) json['mutable_content'] = this.mutableContent;
146146
if (this.additionalData != null) json['data'] = this.additionalData;
@@ -164,9 +164,9 @@ class OSCreateNotification extends JSONStringRepresentable {
164164

165165
// adds buttons
166166
if (this.buttons != null) {
167-
json['buttons'] = this.buttons.map((btn) {
168-
return btn.mapRepresentation();
169-
});
167+
var btns = List<Map<String, dynamic>>();
168+
this.buttons.forEach((btn) => btns.add(btn.mapRepresentation()));
169+
json['buttons'] = btns;
170170
}
171171

172172
return json;

lib/src/notification.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class OSNotification extends JSONStringRepresentable {
4141
this.shown = json['shown'] as bool;
4242
this.appInFocus = json['appInFocus'] as bool;
4343
this.silent = json['silent'] as bool;
44+
4445
if (json.containsKey("androidNotificationId")) this.androidNotificationId = json['androidNotificationId'] as int;
4546
if (json.containsKey('displayType')) this.displayType = OSNotificationDisplayType.values[json['displayType'] as int];
4647
}
@@ -242,7 +243,10 @@ class OSNotificationPayload extends JSONStringRepresentable {
242243
if (json.containsKey('body')) this.body = json['body'] as String;
243244
if (json.containsKey('launchUrl')) this.launchUrl = json['launchUrl'] as String;
244245
if (json.containsKey('additionalData')) this.additionalData = json['additionalData'] as Map<dynamic, dynamic>;
245-
if (json.containsKey('backgroundImageLayout')) this.backgroundImageLayout = OSAndroidBackgroundImageLayout(json['backgroundImageLayout'] as Map<dynamic, dynamic>);
246+
247+
if (json.containsKey('backgroundImageLayout')) {
248+
this.backgroundImageLayout = OSAndroidBackgroundImageLayout(json['backgroundImageLayout'] as Map<dynamic, dynamic>);
249+
}
246250

247251
// raw payload comes as a JSON string
248252
if (json.containsKey('rawPayload')) {
@@ -277,7 +281,10 @@ class OSNotificationOpenedResult {
277281
//constructor
278282
OSNotificationOpenedResult(Map<dynamic, dynamic> json) {
279283
this.notification = OSNotification(json['notification'] as Map<dynamic, dynamic>);
280-
this.action = OSNotificationAction(json['action'] as Map<dynamic, dynamic>);
284+
285+
if (json.containsKey('action')) {
286+
this.action = OSNotificationAction(json['action'] as Map<dynamic, dynamic>);
287+
}
281288
}
282289
}
283290

lib/src/utils.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dynamic convertEnumCaseToValue(dynamic key) {
4242

4343
switch (key) {
4444
case OSCreateNotificationDelayOption.lastActive:
45-
return "last-active";
45+
return "last_active";
4646
case OSCreateNotificationDelayOption.timezone:
4747
return "timezone";
4848
}
@@ -63,5 +63,8 @@ dynamic convertEnumCaseToValue(dynamic key) {
6363
abstract class JSONStringRepresentable {
6464
String jsonRepresentation();
6565

66-
String convertToJsonString(Map<String, dynamic> object) => JsonEncoder.withIndent(' ').convert(object);
66+
String convertToJsonString(Map<String, dynamic> object) => JsonEncoder.withIndent(' ')
67+
.convert(object)
68+
.replaceAll("\\n", "\n")
69+
.replaceAll("\\", "");
6770
}

0 commit comments

Comments
 (0)