英文:
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.
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
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 < 33) return;
        if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted)
        {
            this.RequestPermissions(notiPermission, requestLocationId);
        }
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论