您好,您好,新手在这里!我使用的是本地通知,并向我的应用添加了默认代码。它可以在Android中使用,但不能在iOS中使用。知道为什么吗?
我同时尝试了适用于iPhone 8和iPhone 11的iOS模拟器。它可以在我的Android模拟器(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',
);
}
答案 0 :(得分:7)
我在didFinishLaunchingWithOptions方法内的“ AppDelegate.swift”中添加了这一行,它解决了我的问题:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
// 我希望在插件的文档中找到它 它可以解决您的问题!