轻按本机android后台服务通知时Flutter应用不会出现在前台

时间:2019-10-25 06:49:47

标签: android kotlin flutter dart

我是新来学习颤振的人。我的flutter应用程序在kotlin上有一个本地android后台服务,我在该服务上运行套接字,并且每当套接字侦听某些内容时,我都会使用NotificationCompat.Builder生成通知。

但是,当用户点击通知时,我无法将Flutter应用恢复到前台。

我尝试创建一个未决的意图并将其传递给notificationbuilder setContentIntent(),但是点击时没有任何反应。

本机后台服务上的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 个答案:

答案 0 :(得分:0)

您需要在通知有效负载中包含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