【问题标题】:Bad notification for startForeground in Android appAndroid应用程序中startForeground的错误通知
【发布时间】:2019-11-23 23:11:51
【问题描述】:

我正在使用 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 中,我有以下代码,我们使用它来创建用于发送应用通知的通知通道(并且正确地创建了通知通道)。

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);
}

【问题讨论】:

    标签: android xamarin xamarin.forms xamarin.android


    【解决方案1】:

    对于 Xamarin.Forms 和 Xamarin.Android

    ========将此代码放入公共覆盖 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);
        }
    

    快乐编码。 :-)

    【讨论】:

    • 谢谢。 :) 但是“com.Your.project.id”中的 id 是什么?谢谢。
    【解决方案2】:

    服务通知渠道无效

    您正在创建一个通知渠道,但从未在您的NotificationCompat.Builder 中分配它:

    var notification = new NotificationCompat.Builder(context)
       ~~~
       .SetChannelId(ApplicationConstants.ChannelId)
       ~~~
    

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

    【讨论】:

    • 解决了 :)
    • @DomBurf 很高兴它有帮助 ?
    【解决方案3】:

    这是一个镜头解决方案。

    NotificationChannel chan = new NotificationChannel( "my_service_urgent", "My Channel", NotificationImportance.None );
    chan.EnableVibration( false );
    chan.LockscreenVisibility = NotificationVisibility.Secret;
    
    NotificationManager notificationManager = GetSystemService( NotificationService ) as NotificationManager;
    notificationManager.CreateNotificationChannel( chan );
    
    var notification = new Notification.Builder( this, "my_service_urgent" )
        .SetContentTitle( Resources.GetString( Resource.String.app_name ) )
        .SetContentText( Resources.GetString( Resource.String.notification_text ) )
        .SetSmallIcon( Resource.Drawable.ic_stat_name )
        .SetContentIntent( BuildIntentToShowMainActivity() )
        .SetOngoing( true )
        .AddAction( BuildRestartTimerAction() )
        .AddAction( BuildStopServiceAction() )
        .Build();
    
    // SERVICE_RUNNING_NOTIFICATION_ID = 101
    // Enlist this instance of the service as a foreground service
    StartForeground( Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification );
    

    【讨论】:

      【解决方案4】:

      就我而言,这是.SetSmallIcon(Resource.Drawable.icon)的缺失

      将此行添加到 Notification.Builder() 后,它起作用了。

      奇怪的是,我在 Debug 版本中没有遇到问题,而只是在 Release 版本中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-08
        • 1970-01-01
        相关资源
        最近更新 更多