Push notification 颤振:即使应用程序已关闭,也会推送通知

Push notification 颤振:即使应用程序已关闭,也会推送通知,push-notification,flutter,apple-push-notifications,android-notifications,mobile-application,Push Notification,Flutter,Apple Push Notifications,Android Notifications,Mobile Application,我已经用flutter构建了一个应用程序,它的工作原理就像一个提醒。 即使应用程序已关闭,我如何向用户显示通知?对于提醒,我建议使用。它有一个强大的调度api。从本地通知的文件中: 计划何时应显示通知-定期显示 通知(基于间隔)-安排要显示的通知 每天在指定时间-安排每周显示的通知 在指定的日期和时间-当应用程序处于前台、后台或终止时,能够处理用户点击通知的情况 对于推送通知,您可以使用或插件,也可以通过本机实现 编辑:即使应用程序已终止,您也可以根据特定条件触发通知。这可以通过在后台运行dar

我已经用flutter构建了一个应用程序,它的工作原理就像一个提醒。

即使应用程序已关闭,我如何向用户显示通知?

对于提醒,我建议使用。它有一个强大的调度api。从本地通知的文件中:

计划何时应显示通知-定期显示 通知(基于间隔)-安排要显示的通知 每天在指定时间-安排每周显示的通知 在指定的日期和时间-当应用程序处于前台、后台或终止时,能够处理用户点击通知的情况

对于推送通知,您可以使用或插件,也可以通过本机实现

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

我可以在颤振应用程序的后台运行Dart代码吗?是的,你可以 在iOS和Android上的后台进程中运行Dart代码。对于 更多信息,请参阅媒体文章


您可以在颤振中使用计划通知

 var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));
  var androidPlatformChannelSpecifics =
     new AndroidNotificationDetails('your other channel id',
    'your other channel name', 'your other channel description');
  var iOSPlatformChannelSpecifics =
   new IOSNotificationDetails();
   NotificationDetails platformChannelSpecifics = new 
  NotificationDetails(
   androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.schedule(
 0,
 'scheduled title',
 'scheduled body',
 scheduledNotificationDateTime,
 platformChannelSpecifics);

我也面临过这个问题,所以这些是我的经验

在我的情况下:我可以在应用程序恢复或应用程序背景状态中获得通知,但在应用程序关闭状态中,我没有收到通知

在这种情况下,我们的通知主体是:

{notification: {body: null, title: null}, data: {body: hello, title: world}}
要在应用程序关闭状态下接收通知,我们将通知更改为

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

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


我找到了解决这个问题的办法。我们只需要在应用程序类中注册本地通知插件

首先创建一个类FlatterLocalNotificationPluginRegistrant,我在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
    }
}}
现在创建一个扩展应用程序的应用程序类,并实现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
    android:name="com.packagename.Application"/>
这样称呼它

    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();
}
未来myBackgroundMessageHandler(映射消息)异步{ 如果(消息['data']!=null){ 最终数据=消息[‘数据’]; 最终标题=数据[‘标题’]; 最终正文=数据[‘消息’]; 使用DefaultSound(标题、消息)等待显示通知; } 返回Future.value(); }
感谢您的回答,但在我的情况下,我不希望通知自动触发我希望应用程序根据某些条件显示通知,为了实现这一点,我需要我的应用程序在后台工作,即使它已关闭。为此,您需要连接到firebase等后端数据库,在那里,您可以管理通过internet发送的通知。(对所有用户通用/特定)@SlimenTN遵循我的解决方案,我有与您相同的用例。它在工作!!!flatterLocalNotificationsPlugin工作得很好。但我找不到当计划通知发出时如何“做点什么”。有什么想法吗?一个问题:firebase的后台消息处理程序是否会在应用程序从最近的列表中删除时接收数据消息?如果没有,您是否找到解决方法???@KrishnakumarCN如果您仅从FCM发送数据对象,则您将在后台消息处理程序中接收数据消息,但如果您也发送通知对象,则FCM将自动显示通知,然后单击,您将在onResume中获取数据消息(如果应用程序在后台)或在启动方法中(如果应用程序不在最近的列表中)@Malektubaisat我将创建一个存储库并进行更新here@MalekTubaisaht请检查此存储库。请先执行FCM配置部分。您是否可以为java共享Please而不是kotlinI?您认为在后台处理通知的最佳方法是让FCM自动显示来自通知对象的通知然后单击FCM的ONLAUNCH或ONRESUME方法中的获取数据对象。“在Android上启用后台消息处理”与“应用程序关闭时”不同。
    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();
}