在Android上,您需要显示高优先级通知.这将显示向下滑动的通知面板,该面板将出现在锁定屏幕或其他应用程序上方.由于您已经在使用本机代码,因此您可以在此处创建此通知,或向Dart端发送消息(使用MethodChannel进行操作),在此它可以使用flutter_local_notifications插件显示该消息.当用户单击通知时,您的Flutter应用程序将显示在前台.在Java中,您可以使用类似于以下代码:
// 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 /文件夹下
GeneratedPluginRegistrant.registerWith(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
}
(即使屏幕被锁定,这也会显示Flutter应用程序.)
Flutter只有一个活动,因此上面的代码会将Flutter活动带到前台(请注意,您并不总是看到通知,但是有时您会看到-如果将其设置为autoCancel然后触摸它可以清除它).由您决定在Flutter中构建正确的屏幕,您可以在发送通知时执行此操作.使用Navigator.push或同等功能来更改Flutter显示的页面.