File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11ONESIGNAL_APP_ID = your-onesignal-app-id
22ONESIGNAL_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 =
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11import 'dart:convert' ;
22
33import 'package:flutter/foundation.dart' ;
4+ import 'package:flutter_dotenv/flutter_dotenv.dart' ;
45import 'package:http/http.dart' as http;
56
67import '../models/notification_type.dart' ;
78import '../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+
924class 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 (
Original file line number Diff line number Diff line change 11ONESIGNAL_APP_ID = your-onesignal-app-id # default app id: 77e32082-ea27-42e3-a898-c72e141824ef if empty
22ONESIGNAL_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 =
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11import 'dart:convert' ;
22
33import 'package:flutter/foundation.dart' ;
4+ import 'package:flutter_dotenv/flutter_dotenv.dart' ;
45import 'package:http/http.dart' as http;
56
67import '../models/notification_type.dart' ;
78import '../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+
924class 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 (
You can’t perform that action at this time.
0 commit comments