AlexSJC
发布于 2023-11-10 / 4 阅读
0
0

Xamarin.Android 前台服务

public const string CHANNEL_ID = "default_channel";
public const string CHANNEL_NAME = "Default Channel";
public const int NOTIFICATION_ID = 10000;

public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFlags flags, int startId)
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
    {
        ((NotificationManager)GetSystemService(NotificationService))
            .CreateNotificationChannel(new(CHANNEL_ID, CHANNEL_NAME, NotificationImportance.Default));
    }
    var notification = new NotificationCompat.Builder(this, CHANNEL_ID)
        .SetContentTitle(GetString(Resource.String.app_name))
        .SetContentText(GetString(Resource.String.app_text))
        .SetSmallIcon(Resource.Mipmap.appicon)
        .Build();
    StartForeground(NOTIFICATION_ID, notification);
    return StartCommandResult.Sticky;
}


评论