Flutter:即使应用已关闭,也推送通知

时间:2018-12-01 15:07:20

标签: push-notification flutter apple-push-notifications android-notifications mobile-application

我用颤动构建了一个应用程序,就像提醒一样。
即使该应用已关闭,如何向用户显示通知?

5 个答案:

答案 0 :(得分:11)

我找到了解决此问题的方法。我们只需要在Application类中注册本地通知插件即可。

首先创建一个FlutterLocalNotificationPluginRegistrant类,我已经在Kotlin中创建了它。

class FlutterLocalNotificationPluginRegistrant {

companion object {
    fun registerWith(registry: PluginRegistry) {
        if (alreadyRegisteredWith(registry)) {
            Log.d("Local Plugin", "Already Registered");
            return
        }
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"))
        Log.d("Local Plugin", "Registered");
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
        val key = FlutterLocalNotificationPluginRegistrant::class.java.canonicalName
        if (registry.hasPlugin(key)) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}}

现在创建一个扩展FlutterApplication的Application类,并实现PluginRegistry.PluginRegistrantCallback。

class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {

override fun onCreate() {
    super.onCreate()
}

override fun registerWith(registry: PluginRegistry?) {
    if (registry != null) {
        FlutterLocalNotificationPluginRegistrant.registerWith(registry)
    }
}}

并在AndroidManifest.xml中注册Application类

<application
    android:name="com.packagename.Application"/>

全部完成。现在,编写一个显示通知的函数,并从Firebase消息传递的后台处理程序方法中调用它。

    Future _showNotificationWithDefaultSound(String title, String message) async {
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'channel_id', 'channel_name', 'channel_description',
      importance: Importance.Max, priority: Priority.High);
  var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
   await flutterLocalNotificationsPlugin.show(
    0,
    '$title',
    '$message',
    platformChannelSpecifics,
    payload: 'Default_Sound',
  );
}

并这样称呼它。

    Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {

  if (message['data'] != null) {
    final data = message['data'];

    final title = data['title'];
    final body = data['message'];

    await _showNotificationWithDefaultSound(title, message);
  }
  return Future<void>.value();
}

答案 1 :(得分:5)

为提醒起见,我建议使用Flutter Local Notifications Plugin。它具有强大的调度API。从本地通知的文档中:

  

安排应在何时出现通知-定期显示   通知(基于时间间隔)-安排要显示的通知   每天在指定时间-安排每周显示一次通知   在指定的日期和时间-当应用程序是前台,后台或终止

时,当用户点击通知时的功能

对于推送通知,您可以使用Firebase Cloud Messaging one signal插件,也可以通过platform-channels

本地实现

编辑:即使应用终止,您也可以根据特定条件触发通知。这可以通过在后台运行飞镖代码来解决。引用官方常见问题解答:

  

我可以在Flutter应用程序的后台运行Dart代码吗?是的你可以   在iOS和Android上的后台进程中运行Dart代码。对于   有关更多信息,请参见中型文章Executing Dart in the Background with Flutter Plugins and Geofencing.

答案 2 :(得分:1)

我也遇到了这个问题,所以这些是我的经验教训

在我的情况下:我可以在App继续状态或App后台状态下收到通知,但是在App关闭状态下,我没有收到通知。

在这种情况下,我们的通知正文为:

{notification: {body: null, title: null}, data: {body: hello, title: world}}

要在“应用关闭”状态下接收通知,我们将通知更改为

{notification: {body: abc, title: abc}, data: {url: string, body: string, title: string}}

答案 3 :(得分:0)

您可以快速使用计划的通知。

for each_value in table:
    if each_value[0] == x:
        print("Yeah! Find: {0}".format(each_value[0]))
        break

答案 4 :(得分:0)

firebase_messaging官方flutter插件支持显示通过FCM发送的通知。 最近在v5.1.5中,他们增加了对andriod的后台消息处理的支持。

https://pub.dev/packages/firebase_messaging#515

相关问题