1 回答

TA貢獻1777條經驗 獲得超3個贊
在 Android 上,您需要顯示高優先級通知。這將顯示將出現在鎖定屏幕或其他應用程序上的向下滑動通知面板。由于您已經在使用本機代碼,您可以在那里創建此通知,或向 Dart 端發送一條消息(就像您正在做的那樣,使用MethodChannel),在那里它可以使用flutter_local_notifications插件來顯示它。當用戶單擊通知時,您的顫振應用程序將被帶到前臺。在 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 正在顯示的頁面。
- 1 回答
- 0 關注
- 128 瀏覽
添加回答
舉報