亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從本機后臺服務啟動屏幕

從本機后臺服務啟動屏幕

C#
尚方寶劍之說 2022-07-10 10:18:23
我有一個簡單的 Flutter 應用程序,它使用MethodChannel. BasicMessageChannel<String>當捕獲到特定的本機信息時,此本機后臺服務會通知我的 Flutter 應用程序使用以顯示文本。當我的應用程序在前臺時,所有這些都可以完美運行。當其他應用程序處于前臺時,我無需切換到我的應用程序就看不到文本。我希望我的本機服務可以顯示特定的 Flutter 屏幕,即使其他應用程序正在前臺運行。它可能被認為對用戶不友好,但這是最重要的信息。歡迎任何建議或解決方案!注意:本機服務目前僅在 Java for Android 中可用,我正在為 IOS 端開發 C#。
查看完整描述

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 正在顯示的頁面。


查看完整回答
反對 回復 2022-07-10
  • 1 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號