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

Dom*_*urf 5 android xamarin.android xamarin xamarin.forms

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

这是服务的代码。

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;
}
Run Code Online (Sandbox Code Playgroud)

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

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

小智 5

对于 Xamarin.Forms 和 Xamarin.Android

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

 if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
      RegisterForegroundServiceO();
  else { RegisterForegroundService(); }
Run Code Online (Sandbox Code Playgroud)

=========================================结束========= ===================

 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);
    }
Run Code Online (Sandbox Code Playgroud)

快乐编码。:-)


Sus*_*ver 4

服务通知渠道无效

您正在创建一个通知渠道,但从未将其分配到您的NotificationCompat.Builder

var notification = new NotificationCompat.Builder(context)
   ~~~
   .SetChannelId(ApplicationConstants.ChannelId)
   ~~~
Run Code Online (Sandbox Code Playgroud)

文档:https ://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder