从本机后台服务启动屏幕

我有一个简单的 Flutter 应用程序,它使用MethodChannelBasicMessageChannel<String>当捕获到特定的本机信息时,此本机后台服务会通知我的 Flutter 应用程序使用以显示文本。当我的应用程序在前台时,所有这些都可以完美运行。当其他应用程序处于前台时,我无需切换到我的应用程序就看不到文本。

我希望我的本机服务可以显示特定的 Flutter 屏幕,即使其他应用程序正在前台运行。

它可能被认为对用户不友好,但这是最重要的信息。

欢迎任何建议或解决方案!

注意:本机服务目前仅在 Java for Android 中可用,我正在为 IOS 端开发 C#。


尚方宝剑之说
浏览 109回答 1
1回答

慕森王

在 Android 上,您需要显示高优先级通知。这将显示将出现在锁定屏幕或其他应用程序上的向下滑动通知面板。由于您已经在使用本机代码,您可以在那里创建此通知,或向 Dart 端发送一条消息(就像您正在做的那样,使用MethodChannel),在那里它可以使用flutter_local_notifications插件来显示它。当用户单击通知时,您的颤振应用程序将被带到前台。在 Java 中,您可能会使用类似于以下的代码:// Create an intent which triggers the fullscreen notificationIntent intent = new Intent(Intent.ACTION_MAIN, null);intent.setAction("SELECT_NOTIFICATION");Class mainActivityClass = getMainActivityClass(context);intent.setClass(context, mainActivityClass);PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);// Build the notification as an ongoing high priority item to ensures it will show as// a heads up notification which slides down over top of the current content.final Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID);builder.setOngoing(true);// Set notification content intent to take user to fullscreen UI if user taps on the// notification body.builder.setContentIntent(pendingIntent);// Set full screen intent to trigger display of the fullscreen UI when the notification// manager deems it appropriate.builder.setFullScreenIntent(pendingIntent, true);// Setup notification content.int resourceId = context.getResources().getIdentifier("app_icon", "drawable", context.getPackageName());builder.setSmallIcon(resourceId);builder.setContentTitle("Your notification title");builder.setContentText("Your notification content.");MyPlugin.notificationManager().notify(someId, builder.build());然后,对于 Android 8.1 或更高版本,将以下内容添加到您的MainActivity类中,在 android/app/src/main/java/ packageName / 文件夹下找到GeneratedPluginRegistrant.registerWith(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {&nbsp; setShowWhenLocked(true);&nbsp; setTurnScreenOn(true);}(即使屏幕被锁定,这也会显示 Flutter 应用程序。)Flutter 只有一个活动,因此上面的代码会将 Flutter 活动带到前台(请注意,您并不总是看到通知,但有时您会看到 - 如果将其设置为 autoCancel 然后触摸它会清除它)。在 Flutter 中构建正确的屏幕取决于您,您可以在发送通知时执行此操作。使用Navigator.push或等效更改 Flutter 正在显示的页面。
打开App,查看更多内容
随时随地看视频慕课网APP