Xamarin Forms推送通知未显示在Android通知栏中。

huangapple go评论119阅读模式
英文:

Xamarin Forms push notification not showing up in Android notification bar

问题

我有一个奇怪的问题。我有一个干净的Xamarin Forms项目,使用Plugin.FirebasePushNotification,在iOS上完美运行。奇怪的问题是,它在Android上也能工作,当我从Postman或Firebase发送推送时,会触发OnNotificationReceived,但通知栏中没有任何内容,也没有任何推送声音。

我尝试关闭应用程序,但什么都没有发生,我还尝试“杀死”应用程序,但情况一样。只有当应用程序处于调试模式时,我才能看到OnNotificationReceived实际上已被触发,显示了我发送的消息,所以我的发送负载没有问题。

MainActivity.cs(Android项目)

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

    FirebaseApp.InitializeApp(this);
    FirebasePushNotificationManager.Initialize(this, true);

    LoadApplication(new App());
}

MainPage.xaml.cs(共享项目)

public MainPage()
{
    InitializeComponent();

    //注册推送
    CrossFirebasePushNotification.Current.RegisterForPushNotifications();
    CrossFirebasePushNotification.Current.Subscribe("TESTINGPUSH");
}
英文:

I have a weird problem. I have a clean Xamarin Forms project using Plugin.FirebasePushNotification which is working flawless on iOS. The wierd problem is that it is working on Android as well, OnNotificationReceived is triggered when I send a push from Postman or Firebase however nothing shows up in the notification bar nor does any push sound.

I have tried closing the app yet noting, and I have also tried to "kill" the app but same thing happends. Only when I have app in debug mode I can see the OnNotificationReceived is actually beeing triggered with the message I've sent so there is nothing wrong with my sending payload.

MainActivity.cs (Android-project)

 protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        FirebaseApp.InitializeApp(this);
        FirebasePushNotificationManager.Initialize(this, true);

        LoadApplication(new App());
    }

MainPage.xaml.cs (Shared-project)

public MainPage()
    {
        InitializeComponent();

        //Register for push
        CrossFirebasePushNotification.Current.RegisterForPushNotifications();
        CrossFirebasePushNotification.Current.Subscribe("TESTINGPUSH");
    }

答案1

得分: 1

我终于弄清楚了!

虽然我在我的 manifest.xml 中使用了正确的权限,但它没有考虑到。

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

我在 MainActivity 中进行了重写,然后它开始按预期工作。

protected override void OnStart()
{
    base.OnStart();
    const int requestLocationId = 0;

    string[] notiPermission =
    {
        Manifest.Permission.PostNotifications
    };

    if ((int)Build.VERSION.SdkInt < 33) return;

    if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted)
    {
        this.RequestPermissions(notiPermission, requestLocationId);
    }
}
英文:

I finally figured it out!

although I did use the correct permission in my manifest.xml it didnt take it into account.

&lt;uses-permission android:name=&quot;android.permission.POST_NOTIFICATIONS&quot;/&gt;

I did an override in MainActivity and it started to work as expected.

protected override void OnStart()
    {
        base.OnStart();
        const int requestLocationId = 0;

        string[] notiPermission =
        {
            Manifest.Permission.PostNotifications
        };

        if ((int)Build.VERSION.SdkInt &lt; 33) return;

        if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted)
        {
            this.RequestPermissions(notiPermission, requestLocationId);
        }
    }

huangapple
  • 本文由 发表于 2023年2月26日 23:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定