当前位置: 首页 > 知识库问答 >
问题:

Flutter应用程序在原生android后台服务通知中点击后不会出现在前台

张华池
2023-03-14

我是初学扑动的。我的flutter应用程序在kotlin上有一个原生的android后台服务,在那里我有一个套接字在运行,每当套接字侦听一些东西时,我就会使用notificationcompat.builder生成一个通知。

但当用户点击通知时,我无法将Flutter应用程序带回前台。

我尝试创建一个pendingintent并将其传递给notificationbuilder setContentIntent(),但在Tap上什么也没有发生。

本机后台服务上的shownotification函数:

fun shownotification (){
   packageNamer = this.getPackageName()
   launchIntent = this.getPackageManager().getLaunchIntentForPackage(packageName)
   var className = launchIntent?.getComponent()!!.className.javaClass
   intent.action = "SELECT_NOTIFICATION"

   intent.setClass(this,className)
   pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    var builder = NotificationCompat.Builder(this, "notifications")
            .setOngoing(true)
            .setContentIntent(pendingIntent)
            .setFullScreenIntent(pendingIntent, true)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentTitle("My notification")
            .setContentText("Much longer text that cannot fit one line...")
            .setStyle(NotificationCompat.BigTextStyle()
            .bigText("Much longer text that cannot fit one line..."))
            .setPriority(NotificationCompat.PRIORITY_HIGH)

    manager?.notify(456,builder.build())

}

AndroidManifest.xml文件:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
     calls FlutterMain.startInitialization(this); in its onCreate method.
     In most cases you can leave this as-is, but you if you want to provide
     additional functionality it is fine to subclass or reimplement
     FlutterApplication and put your custom class here. -->
<application
    android:name=".MyApplication"
    android:label="bizzyb_demo"
    android:icon="@mipmap/ic_launcher">
    <service android:name=".MyService" android:exported="true" android:enabled="true"/>

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- This keeps the window background of the activity showing
             until Flutter renders its first frame. It can be removed if
             there is no splash screen (such as the default splash screen
             defined in @style/LaunchTheme). -->
        <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>
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

您的专家反馈将非常感谢。谢谢

共有1个答案

施辉
2023-03-14

您需要在通知负载中包含click_action:flutter_notification_click作为“自定义数据”键值对。

Bundle bundle = new Bundle();
bundle.putString(Constants.EXAM_ID,String.valueOf(lectureDownloadStatus.getExamId()));

var builder = NotificationCompat.Builder(this, "notifications")
            .setOngoing(true)
            .setContentIntent(pendingIntent)
            .setFullScreenIntent(pendingIntent, true)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentTitle("My notification")
            .setContentText("Much longer text that cannot fit one line...")
            .setStyle(NotificationCompat.BigTextStyle()
            .bigText("Much longer text that cannot fit one line..."))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setExtras(bundle) // Set the bundle

 类似资料:
  • 我正在从google firebase为我的android应用程序发送推送通知,目标是Android5.0: 我的推送通知代码是: 但为什么?这就像当应用程序在后台时,通知不使用活动代码中的设置,而只使用AndroidManifest中的某种“默认”设置。

  • 有时当应用程序收到通知时,它不会发出声音。下面是代码段。

  • 我试图在react native中运行一个势不可挡的后台服务,它将通过套接字持续侦听消息。来自服务器的io。 为此,我找到了几个解决方案和几个问题 > 这东西很不错,但它只允许每5分钟运行一次作业,如果你的服务被Android杀死,你就消失了 老实说,我无法理解它的文档 请告诉我该怎么办,我很困惑,很惊讶没有合适的方法来处理这个问题

  • 问题内容: 每当用户断开手机通话时,我都希望显示自定义弹出消息。问题是如何检测应用程序何时未运行。任何线索都将有所帮助。 问题答案: 已经有一段时间了,并且已经有了很多发展。 首先,如何在Flutter中创建服务以使应用程序始终在后台运行有一些答案? 此外,使用Flutter插件和地理围栏(在2018年9月),Flutter / Background流程 基本上将使您指向在后台执行中等/执行Dar

  • 我正在尝试构建一个实时聊天应用程序。 我已经整合了https://pub.dev/packages/flutter_local_notifications用于推送通知的软件包,这是有效的。 我没有使用Firebase,我正在使用我自己的自定义后端,使用https://socket.io/进行实时聊天。 我想在用户发送聊天信息时接收推送通知。推送通知在应用程序处于前台或后台时起作用。但是,当我从进程