如何在Flutter中安排通知? [英] How do I schedule a notification in Flutter?

查看:64
本文介绍了如何在Flutter中安排通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 flutter_local_notifications 包,并且正在尝试在Android设备中安排通知。 / p>

我使用此软件包成功创建了一个通知,但预定的通知似乎都不对我有用



我的AndroidManifest .xml如下所示:

 <清单xmlns:android = http://schemas.android.com/apk/ res / android 
package = ...>
< uses-permission android:name = android.permission.INTERNET />
< uses-permission android:name = android.permission.RECEIVE_BOOT_COMPLETED />
< uses-permission android:name = android.permission.VIBRATE />

< receiver android:name = com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver />
< receiver android:name = com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver>
< intent-filter>
< action android:name = android.intent.action.BOOT_COMPLETED>< / action>
< / intent-filter>
< / receiver>
<应用程序
android:name = io.flutter.app.FlutterApplication
android:label = notificaciones
android:icon = @ mipmap / ic_launcher> ;
< activity
android:name =。MainActivity
android:launchMode = singleTop
android:theme = @ style / LaunchTheme
android: configChanges = orientation | keyboardHidden | keyboard | screenSize | locale | layoutDirection | fontScale | screenLayout | density
android:hardwareAccelerated = true
android:windowSoftInputMode = adjustResize>
<元数据
android:name = io.flutter.app.android.SplashScreenUntilFirstFrame
android:value = true />
< intent-filter>
< action android:name = android.intent.action.MAIN />
< category android:name = android.intent.category.LAUNCHER />
< / intent-filter>
< / activity>
< / application>



我添加了 android.enableAapt2 = false 到gradle.properties,原因是



AAPT:错误:未知元素<接收者>找到,如​​此线程所述,以避免构建错误



我的代码如下:

  import'package: flutter / material.dart'; 
import‘package:flutter_local_notifications / flutter_local_notifications.dart’;

void main()=> runApp(new MyApp());

类MyApp扩展了StatelessWidget {
@override
Widget build(BuildContext context){
return new MaterialApp(
title:'Flutter Demo',
主题:new ThemeData(
primarySwatch:Colors.blue,
),
主页:new MyHomePage(),
);
}
}

类MyHomePage扩展了StatefulWidget {

@override
_MyHomePageState createState()=>新的_MyHomePageState();
}

类_MyHomePageState扩展State< MyHomePage> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

Future onSelectNotification(字符串有效载荷){
showDialog(
context:context,
builder:(_)=> AlertDialog(
title:Text ( ALERT),
内容:Text( CONTENT:$ payload),
));
}

showNotification()异步{
var android = AndroidNotificationDetails(
频道ID,频道名称,频道说明);
var iOS = IOSNotificationDetails();
var platform = NotificationDetails(android,iOS);
varcheduleNotificationDateTime =
new DateTime.now()。add(Duration(seconds:10));
等待flutterLocalNotificationsPlugin.schedule(0, Title, Body,cheduledNotificationDateTime,平台);
}

@override
void initState(){
super.initState();
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var android = AndroidInitializationSettings(’@ mipmap / ic_launcher’);
var iOS = IOSInitializationSettings();
var initSettings = InitializationSettings(android,iOS);
flutterLocalNotificationsPlugin.initialize(initSettings,
selectNotification:onSelectNotification);
}

@override
小部件构建(BuildContext上下文){
return new Scaffold(
appBar:new AppBar(
title:new Text( Demo),
),
正文:new Center(
子项:new Column(
mainAxisAlignment:MainAxisAlignment.center,
子项:< Widget> ; [],
),
),
floatActionButton:new FloatingActionButton(
onPressed:showNotification,
child:new Icon(Icons.notifications),
),
);
}
}


解决方案

谢谢到@pskink,我意识到我应该将AndroidManifest.xml的接收者放在application标签内。



因此,最终的AndroidManifest.xml如下所示:

 <清单xmlns:android = http://schemas.android.com/apk/res/android 
package = ...>
< uses-permission android:name = android.permission.INTERNET />
< uses-permission android:name = android.permission.RECEIVE_BOOT_COMPLETED />
< uses-permission android:name = android.permission.VIBRATE />
<应用程序
android:name = io.flutter.app.FlutterApplication
android:label = notificaciones
android:icon = @ mipmap / ic_launcher> ;
< receiver android:name = com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver />
< receiver android:name = com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver>
< intent-filter>
< action android:name = android.intent.action.BOOT_COMPLETED>< / action>
< / intent-filter>
< / receiver>
< activity
android:name =。MainActivity
android:launchMode = singleTop
android:theme = @ style / LaunchTheme
android: configChanges = orientation | keyboardHidden | keyboard | screenSize | locale | layoutDirection | fontScale | screenLayout | density
android:hardwareAccelerated = true
android:windowSoftInputMode = adjustResize>
<元数据
android:name = io.flutter.app.android.SplashScreenUntilFirstFrame
android:value = true />
< intent-filter>
< action android:name = android.intent.action.MAIN />
< category android:name = android.intent.category.LAUNCHER />
< / intent-filter>
< / activity>
< / application>



和<$ c $ gradle.properties中的c> android.enableAapt2 = false 不再需要。
其余代码保持不变。


Im using the flutter_local_notifications package and im trying to schedule a notification in my Android device.

I successfully created a notification using this package but neither of the scheduled ones seems to work for me

My AndroidManifest.xml looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="...">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="notificaciones"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

I added android.enableAapt2=false to the gradle.properties due to

AAPT: error: unknown element <receiver> found as mentioned in this thread to avoid a build error.

My code looks like this:

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  Future onSelectNotification(String payload) {
    showDialog(
        context: context,
        builder: (_) => AlertDialog(
              title: Text("ALERT"),
              content: Text("CONTENT: $payload"),
            ));
  }

  showNotification() async {
    var android = AndroidNotificationDetails(
        'channel id', 'channel name', 'channel description');
    var iOS = IOSNotificationDetails();
    var platform = NotificationDetails(android, iOS);
    var scheduledNotificationDateTime =
        new DateTime.now().add(Duration(seconds: 10));
    await flutterLocalNotificationsPlugin.schedule(0, 'Title ', 'Body', scheduledNotificationDateTime, platform);
  }

  @override
  void initState() {
    super.initState();
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var android = AndroidInitializationSettings('@mipmap/ic_launcher');
    var iOS = IOSInitializationSettings();
    var initSettings = InitializationSettings(android, iOS);
    flutterLocalNotificationsPlugin.initialize(initSettings,
        selectNotification: onSelectNotification);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Demo"),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: showNotification,
        child: new Icon(Icons.notifications),
      ),
    );
  }
}

解决方案

Thanks to @pskink , I realized that I am supposed to put the receiver of the AndroidManifest.xml inside the application tag.

So the final AndroidManifest.xml stands like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="...">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="notificaciones"
    android:icon="@mipmap/ic_launcher">
    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

and theandroid.enableAapt2=false in the gradle.properties is not needed anymore. The rest of the code stays as it is.

这篇关于如何在Flutter中安排通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆