Flutter 本地通知不适用于 iOS
Flutter local notification doesn't work for iOS
这里是 flutter 新手,您好!我正在使用 flutter 本地通知并将默认代码添加到我的应用程序。它在 Android 中有效,但在 iOS 中无效。知道为什么吗?
我尝试了 iOS 模拟器 iPhone 8 和 iPhone 11。它适用于我的 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',
);
}
我在 didFinishLaunchingWithOptions 方法中的“AppDelegate.swift”中添加了这一行,它解决了我的问题:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
//
我希望在插件的文档中找到这个
它解决了您的问题!
这里是 flutter 新手,您好!我正在使用 flutter 本地通知并将默认代码添加到我的应用程序。它在 Android 中有效,但在 iOS 中无效。知道为什么吗?
我尝试了 iOS 模拟器 iPhone 8 和 iPhone 11。它适用于我的 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',
);
}
我在 didFinishLaunchingWithOptions 方法中的“AppDelegate.swift”中添加了这一行,它解决了我的问题:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
// 我希望在插件的文档中找到这个 它解决了您的问题!