【发布时间】:2020-06-14 22:46:26
【问题描述】:
你好颤振新手在这里!我正在使用颤振本地通知并将默认代码添加到我的应用程序中。它适用于Android,但不适用于iOS。知道为什么吗?
我尝试使用适用于 iPhone 8 和 iPhone 11 的 iOS 模拟器。它适用于我的 Pixel 3 API 29 安卓模拟器。
我还为此应用启用了 iOS 中的通知(设置>此应用>通知)并授予权限。
对于 Android,我在 AndroidManifest.xml 中添加了权限请求。
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class AppNotification extends StatefulWidget {
static const String id = 'app_notification';
@override
_AppNotificationState createState() => _AppNotificationState();
}
class _AppNotificationState extends State<AppNotification> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
@override
void initState() {
super.initState();
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var android = AndroidInitializationSettings('app_icon');
var iOS = IOSInitializationSettings();
var initSettings = InitializationSettings(android, iOS);
flutterLocalNotificationsPlugin.initialize(initSettings,
onSelectNotification: onSelectNotification);
}
Future onSelectNotification(String payload) async {
debugPrint("payload : $payload");
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text("PayLoad"),
content: Text("Payload : $payload"),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text('Notification'),
onPressed: _showNotificationWithDefaultSound,
),
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('back'),
)
],
),
),
);
}
// TODO iOS doesn't work
Future _showNotificationWithDefaultSound() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'New Post',
'How to Show Notification in Flutter',
platformChannelSpecifics,
payload: 'Default_Sound',
);
}
【问题讨论】:
-
嘿,我在 iOS 上遇到了同样的问题,当我点击一个按钮时,什么也没有发生。你找到解决办法了吗?
标签: flutter notifications