gpt4 book ai didi

android - Android 应用程序中 startForeground 的错误通知

转载 作者:行者123 更新时间:2023-12-03 03:20:50 27 4
gpt4 key购买 nike

我正在使用 Xamarin Android 3.5 开发一项服务。我们的应用程序面向 Android 8.1 (API 27 - Oreo)。我希望该服务作为前台服务运行。但是,当我运行该服务时,出现以下错误。

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)

这是该服务的代码。

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
base.OnStartCommand(intent, flags, startId);
var context = Application.Context;
const int pendingIntentId = 0;
PendingIntent pendingIntent = PendingIntent.GetActivity(context, pendingIntentId, intent, PendingIntentFlags.OneShot);
var notification = new NotificationCompat.Builder(context)
.SetContentTitle("Testing")
.SetContentText("location tracking has begun.")
.SetSmallIcon(Resource.Drawable.icon)
.SetContentIntent(pendingIntent)
.SetOngoing(true)
.Build();
// Enlist this instance of the service as a foreground service
const int Service_Running_Notification_ID = 935;
StartForeground(Service_Running_Notification_ID, notification);
return StartCommandResult.NotSticky;
}

我已使用以下内容更新了 AndroidManifest.xml

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

MainActivity.c中,我有以下代码,我们用它来创建用于发送应用程序通知的通知 channel (并且正确地创建了通知 channel )。

private void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel(ApplicationConstants.ChannelId, ApplicationConstants.ChannelName, NotificationImportance.Default)
{
Description = ApplicationConstants.ChannelDescription
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}

最佳答案

对于 Xamarin.Forms 和 Xamarin.Android

========将此代码放在公共(public)重写 StartCommandResult OnStartCommand ===========

 if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
RegisterForegroundServiceO();
else { RegisterForegroundService(); }

==========================================END====== =======================

 void RegisterForegroundService()
{
var notification = new Notification.Builder(this)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.icon_userProfile)
.SetContentIntent(BuildIntentToShowMainActivity())
.SetOngoing(true)
.Build();
const int Service_Running_Notification_ID = 936;
StartForeground(Service_Running_Notification_ID, notification);
}


void RegisterForegroundServiceO()
{
String NOTIFICATION_CHANNEL_ID = "com.Your.project.id";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Channel Name", NotificationManager.ImportanceHigh);

NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);

manager.CreateNotificationChannel(chan);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

Notification notification= notificationBuilder.SetOngoing(true)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.icon_userProfile)
.SetContentIntent(BuildIntentToShowMainActivity())
.SetOngoing(true)
.Build();

const int Service_Running_Notification_ID = 936;
StartForeground(Service_Running_Notification_ID, notification);
}

快乐编码。 :-)

关于android - Android 应用程序中 startForeground 的错误通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com