从本机后台服务启动屏幕 [英] Launch screen from native background service

查看:111
本文介绍了从本机后台服务启动屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Flutter应用程序,它使用 MethodChannel 启动本机后台服务。当捕获到特定的本机信息时,此本机后台服务使用 BasicMessageChannel< String> 通知我的Flutter应用程序以显示文本。当我的应用程序处于前台时,所有这些功能都可以完美地工作。当其他应用程序处于前台时,我必须切换到我的应用程序才能看到文本。

I have a simple Flutter app that start a native background service using MethodChannel. This native background service notify my Flutter app using BasicMessageChannel<String> to display text when a particular native information is catched. All of this work perfectly when my app is in foreground. When an other app is in foreground, I can't see the text without having to switch to my app.

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

I want that my native service can display a particular Flutter screen even if an other application is running in foreground.

它可以被认为不是用户友好的,但这是至关重要的信息。

It can be perceived as not user friendly, but this is an message of utmost importance.

任何建议或解决方案都将受到欢迎!

Any suggestion or solution will be welcome !

注意:本机服务目前仅适用于Java for Android,我

推荐答案

在Android上,您需要显示高优先级通知。这将显示向下滑动的通知面板,该面板将出现在锁定屏幕或其他应用程序上方。由于您已经在使用本机代码,因此可以在此处创建此通知,或向Dart端发送消息(使用 MethodChannel 进行操作),以便在其中使用 flutter_local_notifications 插件进行显示。当用户单击通知时,您的Flutter应用程序将显示在前台。在Java中,您可能会使用类似于以下代码:

On Android you need to display a high priority notification. This displays the slide-down notification panel which will appear over the lock screen or another app. Since you are using native code already, you can create this notification there, or send a message to the Dart side (as you are doing, using MethodChannel) where it can use the flutter_local_notifications plugin to display it. When the user click the notification, your flutter app is brought to the foreground. In Java you might use code similar to this:

// Create an intent which triggers the fullscreen notification
Intent 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 /文件夹下

Then, for Android 8.1 or higher, add the following to your MainActivity class, found under the android/app/src/main/java/packageName/ folder

GeneratedPluginRegistrant.registerWith(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
  setShowWhenLocked(true);
  setTurnScreenOn(true);
}

(即使屏幕锁定,它也会显示Flutter应用程序。)

(This shows the Flutter app even when the screen is locked.)

Flutter仅具有一个活动,因此上述代码会将Flutter活动置于前台(请注意,您并不总是看到通知,但有时您会看到该通知-如果您将其设置为autoCancel,然后触摸即可清除)。由您决定在Flutter中建立正确的屏幕,您可以在发送通知时进行操作。使用 Navigator.push 或等效的按钮来更改Flutter显示的页面。

Flutter only has one activity, so the above code will bring that Flutter activity to the foreground (note that you don't always see the notification, but sometimes you do - if you set it to autoCancel then touching it clears it). It's up to you to build the correct screen in Flutter, which you can do as you send the notification. Use Navigator.push or equivalent to change the page that Flutter is showing.

这篇关于从本机后台服务启动屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆