當啟動一個前臺服務時,我得到了這個錯誤:
Java.Lang.RuntimeException:
我得到了這個錯誤。
Java.Lang.RuntimeException。startForeground的壞通知:java.lang.RuntimeException:無效的服務通知。Notification(channel=channelSocketIOService pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
---管理的Java.Lang.RuntimeException堆疊跟蹤結束 ---
android.app.RemoteServiceException。startForeground的壞通知:java.lang.RuntimeException:無效的服務通知。Notification(channel=channelSocketIOService pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
/end;
在服務類中:
private void RegisterForegroundService()
{
Notification notification = new NotificationCompat.Builder(this, $"ServiceNotifications")
.SetOngoing(true)
.Build()。
StartForeground(462, notification);
}
感謝您的幫助
uj5u.com熱心網友回復:
你需要在Android 8及以上版本中創建一個默認的通知通道。否則你會得到這個錯誤。 更多細節見 升級到Android 8.1后startForeground失敗
uj5u.com熱心網友回復:
從檔案創建和管理通知通道中,我們將知道:
從Android 8.0(API級別26)開始,所有的通知都必須被分配到一個頻道。 分配給一個通道。對于每個通道,您可以設定應用于所有通知的視覺和 視覺和聽覺行為,這些行為將應用于該通道中的所有通知。 頻道的所有通知。然后,用戶可以改變這些設定,并決定哪些 你的應用程式的哪些通知通道應該是侵入性的或可見的。
。
在xamarin Android平臺上,您可以查看檔案Xamarin.Android中的通知。
并且在上述代碼中包含了一個示例。 你可以在這里查看。https://github.com/xamarin/monodroid-samples/tree/master/LocalNotifications 。
。主要代碼是:
public class MainActivity : AppCompatActivity
{
// 為我們的通知提供唯一的ID。
static readonly int NOTIFICATION_ID = 1000;
static readonly string CHANNEL_ID = "location_notification";
內部靜態只讀字串 COUNT_KEY = "count"。
// 點擊按鈕的次數(從第一次點擊開始)。
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle)。
SetContentView(Resource.Layout.Main)。
CreateNotificationChannel()。
// 顯示 "Hello World, Click Me!"按鈕并注冊其事件處理程式。
var button = FindById<Button>(Resource.Id.MyButton)。
button.Click = ButtonOnClick;
}
// 按鈕點擊事件的處理程式。
空白 ButtonOnClick(object sender, EventArgs eventArgs)
{
// 將當前的按鈕按下次數值傳遞給下一個活動。
var valuesForActivity = new Bundle();
valuesForActivity.PutInt(COUNT_KEY, count)。
// 當用戶點擊通知時,SecondActivity將啟動。
var resultIntent = new Intent(this, typeof(SecondActivity))。
// 傳遞一些值給SecondActivity。
resultIntent.PutExtras(valuesForActivity)。
// 構建一個用于跨任務導航的后堆疊。
var stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Class.FromType(typeof(SecondActivity)))。
stackBuilder.AddNextIntent(resultIntent)。
// 用后堆疊創建 PendingIntent。
var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int) PendingIntentFlags.UpdateCurrent);
// 構建通知。
var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetAutoCancel(true) // 當用戶點擊通知時,將通知從通知區撤走
.SetContentIntent(resultPendingIntent) // 當用戶點擊該意圖時啟動該活動。
.SetContentTitle("Button Clicked") // 設定標題
.SetNumber(count) // 在內容資訊中顯示計數。
.SetSmallIcon(Resource.Drawable.ic_stat_button_click) // 這是要顯示的圖示
.SetContentText($"該按鈕已被點擊{count}次。"); //顯示的資訊。
// 最后,發布通知。
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(NOTIFICATION_ID, builder.Build())。
// 遞增按下按鈕的次數。
count ;
}
void CreateNotificationChannel()
{
如果(Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// 通知通道是API 26中的新內容(并且不是支持庫的一部分)。
// 支持庫的一部分)。) 沒有必要在舊版本的Android上創建一個通知
// 在舊版本的Android上創建通知通道。
回傳。
}
var name = Resources.GetString(Resource.String.channel_name);
var description = GetString(Resource.String.channel_description);
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default)
{
說明=描述
};
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel)。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317276.html
標籤:
