首页 > 解决方案 > Xamarin Android NotificationCompat 不显示且没有错误

问题描述

使用 Xamarin Forms,目前只是 Android 平台,我正在尝试让通知通过 Azure 和 Firebase 工作。在当前状态下,通知已收到并具有内容,因此它不为空,它只是它说“Azure 测试通知”的基本通知。但是,我整天都在谷歌搜索,由于某种原因,我找不到我的问题的答案:通知不会显示,也没有错误,它只是跳过代码,不做任何事情。

在 MainActivity 中创建通道(我在这里这样做是因为 docs.microsoft 说过)

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 name = Resources.GetString(Resource.String.AppName);
        var description = GetString(Resource.String.Description);
        var channel = new NotificationChannel("4867453", name, NotificationImportance.Default)
        {
            Description = description
        };

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);

在我单独的服务课程中,我有

var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, Channel_ID)
                .SetContentTitle("Test Message")
                .SetContentText(messageBody)
                .SetContentIntent(pendingIntent);

    var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
    notificationManager.Notify(Notification_ID, notification.Build());

其中一些,主要是 Intent 的东西,是我在 Microsoft Docs 上找到的,并且对应用程序的工作没有影响或根本没有影响,我之前已经将其删除以进行测试,但它没有任何影响。

老实说,我可能会做一些愚蠢的事情,但是,如果有人能对我的问题有所了解,我将不胜感激。谢谢 :)。

标签: androidformsfirebasexamarin

解决方案


当我开始使用 Android 9(Pie) 时遇到了类似的问题。

事实证明,在 Android 9.0 中,他们弃用了NotificationHubs Bindings nuget包所依赖的Apache HTTP 客户端。

根据他们的文档:

要继续使用 Apache HTTP 客户端,面向 Android 9 及更高版本的应用可以将以下内容添加到其 AndroidManifest.xml 中:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

添加后,我的通知再次开始工作。

似乎确实有一个GitHub 问题说它已修复,但他们的 nuget 包似乎真的过时了。


推荐阅读