Skip to content

Commit 5b28501

Browse files
committed
chore(examples): add env-based Android channel ID override
1 parent 0c84ddc commit 5b28501

6 files changed

Lines changed: 48 additions & 10 deletions

File tree

examples/demo/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
ONESIGNAL_APP_ID=your-onesignal-app-id
22
ONESIGNAL_API_KEY=your-onesignal-api-key
3+
4+
# Optional: Android Notification Channel ID for the WITH SOUND test notification.
5+
# Create one in your OneSignal dashboard under Settings > Android Notification Categories.
6+
ONESIGNAL_ANDROID_CHANNEL_ID=

examples/demo/lib/models/notification_type.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ enum NotificationType {
1717
title: 'Sound Notification',
1818
body: 'This notification plays a custom sound',
1919
iosSound: 'vine_boom.wav',
20-
androidChannelId: 'b3b015d9-c050-4042-8548-dcc34aa44aa4',
20+
useAndroidChannel: true,
2121
);
2222

2323
final String title;
2424
final String body;
2525
final String? bigPicture;
2626
final Map<String, String>? iosAttachments;
2727
final String? iosSound;
28-
final String? androidChannelId;
28+
final bool useAndroidChannel;
2929

3030
const NotificationType({
3131
required this.title,
3232
required this.body,
3333
this.bigPicture,
3434
this.iosAttachments,
3535
this.iosSound,
36-
this.androidChannelId,
36+
this.useAndroidChannel = false,
3737
});
3838
}

examples/demo/lib/services/onesignal_api_service.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import 'dart:convert';
22

33
import 'package:flutter/foundation.dart';
4+
import 'package:flutter_dotenv/flutter_dotenv.dart';
45
import 'package:http/http.dart' as http;
56

67
import '../models/notification_type.dart';
78
import '../models/user_data.dart';
89

10+
const String _defaultAndroidChannelId = 'b3b015d9-c050-4042-8548-dcc34aa44aa4';
11+
12+
String _resolveAndroidChannelId() {
13+
String? envValue;
14+
try {
15+
envValue = dotenv.env['ONESIGNAL_ANDROID_CHANNEL_ID']?.trim();
16+
} catch (_) {
17+
envValue = null;
18+
}
19+
return (envValue != null && envValue.isNotEmpty)
20+
? envValue
21+
: _defaultAndroidChannelId;
22+
}
23+
924
class OneSignalApiService {
1025
String _appId = '';
1126
String _apiKey = '';
@@ -36,8 +51,8 @@ class OneSignalApiService {
3651
if (type.iosSound != null) {
3752
body['ios_sound'] = type.iosSound;
3853
}
39-
if (type.androidChannelId != null) {
40-
body['android_channel_id'] = type.androidChannelId;
54+
if (type.useAndroidChannel) {
55+
body['android_channel_id'] = _resolveAndroidChannelId();
4156
}
4257

4358
final response = await http.post(

examples/demo_pods/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
ONESIGNAL_APP_ID=your-onesignal-app-id # default app id: 77e32082-ea27-42e3-a898-c72e141824ef if empty
22
ONESIGNAL_API_KEY=your-onesignal-api-key
3+
4+
# Optional: Android Notification Channel ID for the WITH SOUND test notification.
5+
# Create one in your OneSignal dashboard under Settings > Android Notification Categories.
6+
ONESIGNAL_ANDROID_CHANNEL_ID=

examples/demo_pods/lib/models/notification_type.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ enum NotificationType {
1717
title: 'Sound Notification',
1818
body: 'This notification plays a custom sound',
1919
iosSound: 'vine_boom.wav',
20-
androidChannelId: 'b3b015d9-c050-4042-8548-dcc34aa44aa4',
20+
useAndroidChannel: true,
2121
);
2222

2323
final String title;
2424
final String body;
2525
final String? bigPicture;
2626
final Map<String, String>? iosAttachments;
2727
final String? iosSound;
28-
final String? androidChannelId;
28+
final bool useAndroidChannel;
2929

3030
const NotificationType({
3131
required this.title,
3232
required this.body,
3333
this.bigPicture,
3434
this.iosAttachments,
3535
this.iosSound,
36-
this.androidChannelId,
36+
this.useAndroidChannel = false,
3737
});
3838
}

examples/demo_pods/lib/services/onesignal_api_service.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import 'dart:convert';
22

33
import 'package:flutter/foundation.dart';
4+
import 'package:flutter_dotenv/flutter_dotenv.dart';
45
import 'package:http/http.dart' as http;
56

67
import '../models/notification_type.dart';
78
import '../models/user_data.dart';
89

10+
const String _defaultAndroidChannelId = 'b3b015d9-c050-4042-8548-dcc34aa44aa4';
11+
12+
String _resolveAndroidChannelId() {
13+
String? envValue;
14+
try {
15+
envValue = dotenv.env['ONESIGNAL_ANDROID_CHANNEL_ID']?.trim();
16+
} catch (_) {
17+
envValue = null;
18+
}
19+
return (envValue != null && envValue.isNotEmpty)
20+
? envValue
21+
: _defaultAndroidChannelId;
22+
}
23+
924
class OneSignalApiService {
1025
String _appId = '';
1126
String _apiKey = '';
@@ -36,8 +51,8 @@ class OneSignalApiService {
3651
if (type.iosSound != null) {
3752
body['ios_sound'] = type.iosSound;
3853
}
39-
if (type.androidChannelId != null) {
40-
body['android_channel_id'] = type.androidChannelId;
54+
if (type.useAndroidChannel) {
55+
body['android_channel_id'] = _resolveAndroidChannelId();
4156
}
4257

4358
final response = await http.post(

0 commit comments

Comments
 (0)