Kar*_*aar 6 android-notifications flutter
我正在使用 flutter 本地通知插件来通知用户事件开始。
所有事件都存储在 firebase 中,并附有日期和时间。
如果用户将该事件添加到他的日程安排中,他就会收到通知。
我的问题是本地通知插件需要参数中的int ,并且在 firebase 中我有 id 类型 String (该事件与另一个生成字符串 ID 的 web 应用程序一起添加到 firebase 中)。
那么flutter中有没有办法让通知与String ID一起工作呢?
这是数据库:

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);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我不知道通知的 iOS 实现是什么样的,但 Android 实现使用 int。我认为没有办法解决这个问题。
你可以做的就是简单地使用 hashcode 函数为你的 String id 生成一个 int 哈希值:
String eventID = "asdnasjdnaskdnbaihbd2in1en213123";
int notificationId = eventID.hashCode;
Run Code Online (Sandbox Code Playgroud)
hashcode 的文档说:
/**
* 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;
Run Code Online (Sandbox Code Playgroud)
意味着只要您的 eventId 是唯一的,就应该可以正常工作。
| 归档时间: |
|
| 查看次数: |
3730 次 |
| 最近记录: |