【问题标题】:Flutter local notification doesn't work for iOSFlutter 本地通知不适用于 iOS
【发布时间】: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


【解决方案1】:

我在 didFinishLaunchingWithOptions 方法内的“AppDelegate.swift”中添加了这一行,它解决了我的问题:

if #available(iOS 10.0, *) {
    UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

文档链接: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications#%EF%B8%8F-ios-setup

// 我在插件的文档中找到了这个,希望 它解决了你的问题!

【讨论】:

  • 这解决了我的问题,谢谢,这是docs的链接
  • @fixatd,你能把这个方法全部放在这里吗?
  • 这是我的解决方案,谢谢@K-Soliman
猜你喜欢
  • 2021-07-01
  • 2014-08-18
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2022-11-29
  • 2019-02-10
  • 2017-01-22
相关资源
最近更新 更多