Reputation: 101
I am using the flutter local notification plugin to notify the user that the event starts.
All the events are stored in firebase with date and time.
If the user adds the event to his schedule he gets notified.
My problem is that the local notification plugin needs an int in the parameter and in firebase I have the id type String (the event are added in firebase with another webapp which generate String ID).
So is there a way in flutter to make the notification work with a String ID?
This is the database:
lass DetailScreen extends StatefulWidget {
final String id;
DetailScreen(this.id);
@override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
String firstHalf;
String secondHalf;
bool flag = true;
int selectedValue;
bool isScheduled = false;
bool isLoading = true;
initState() {
selectedValue = 0;
Future.delayed(Duration(seconds: 0), () async {
await getStatus();
final response = await flutterLocalNotificationsPlugin
.getNotificationAppLaunchDetails();
if (response.didNotificationLaunchApp) {
await Provider.of<ShowsModel>(context, listen: false).getShows();
await Provider.of<ChannelModel>(context, listen: false).getChannels();
}
setState(() {
isLoading = false;
});
});
super.initState();
}
showNotification(String title, DateTime showTime) async {
var android = AndroidNotificationDetails(
'channel id', 'channel name', 'channel description',
enableVibration: true,
playSound: true,
importance: Importance.High,
priority: Priority.High);
var iOS = IOSNotificationDetails();
var platform = NotificationDetails(android, iOS);
new DateTime.now().add(Duration(seconds: 10));
await flutterLocalNotificationsPlugin.schedule(
int.parse(widget.id),
title,
'The show is about to start in 15 minutes',
showTime.subtract(Duration(minutes: 15)),
platform,
payload: widget.id);
}
Upvotes: 6
Views: 4656
Reputation: 5355
I was using random int for a while and works fine.
await plugin?.show(
Random().nextInt(1000000), // here
notification?.title,
notification?.body,
details,
);
Upvotes: 0
Reputation: 1080
Unfortunately I don't know how the iOS implementation for notifications looks like, but the Android implementation works with an int. And I think there is no way around it.
What you could do is simply use the hashcode function to generate an int hash for your String id:
String eventID = "asdnasjdnaskdnbaihbd2in1en213123";
int notificationId = eventID.hashCode;
The documentation of hashcode says:
/**
* Returns a hash code derived from the code units of the string.
*
* This is compatible with [operator ==]. Strings with the same sequence
* of code units have the same hash code.
*/
int get hashCode;
Means as long as your eventIds are unique this should work fine.
Upvotes: 9