Xamarin推送通知

发布于 2025-02-10 08:53:39 字数 4197 浏览 1 评论 0原文

在后台接收和打开该应用程序的通知会增加发送的数据数。

此外,该通知未显示何时杀死该应用程序或何时在前景中。

以下是我的代码,源代码来自Gerald versluis https:> https://github.com/github.com/jfversluis/jfversluis/xffcmpushnotipationsmplempamp

using System;
using Xamarin.Forms;
using Plugin.FirebasePushNotification;
using Xamarin.Forms.Xaml;

namespace FCMTest
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();

            CrossFirebasePushNotification.Current.Subscribe("all");
            CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;
            CrossFirebasePushNotification.Current.OnNotificationReceived += Current_OnNotificationReceived;
            CrossFirebasePushNotification.Current.OnNotificationOpened += Current_OnNotificationOpened;
           //CrossFirebasePushNotification.Current.OnNotificationAction += Current_OnNotificationAction;
        }

        private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine($"Token from function OnTokenRefresh: {e.Token}");
        }

        private void Current_OnNotificationReceived(object source, FirebasePushNotificationDataEventArgs d)
        {
            System.Diagnostics.Debug.WriteLine("Received");
            foreach (var data in d.Data)
            {
                System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
            }
        }

        /*private void Current_OnNotificationAction(object source, FirebasePushNotificationResponseEventArgs d)
        {
            System.Diagnostics.Debug.WriteLine("Opened");
            foreach (var data in d.Data)
            {
                System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
            }
        }*/

        private void Current_OnNotificationOpened(object source, FirebasePushNotificationResponseEventArgs d)
        {
            System.Diagnostics.Debug.WriteLine("Opened");
            foreach (var data in d.Data)
            {
                System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
            }
        }
        
        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}
using FirebaseAdmin;
using FirebaseAdmin.Messaging;
using Google.Apis.Auth.OAuth2;
using System;
using System.Collections.Generic;


namespace FCMDispatcher
{
    class Program
    {
        static void Main(string[] args)
        {
            FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromFile("fcm-test-98fe5-firebase-adminsdk-oknwc-c290af4ff0.json")
            });

            // This registration token comes from the client FCM SDKs.
            var registrationToken = "d8yECxIhJXQ:APA91bFGXSrnUVcP07TlN4HpvfstWwEdPQaj4wr6Z3Q-7JqcJJjrFy9LkWdlfzcCNDrZeVy55IDTWxvp5Gfyv8318uRRmIPo6Gp2IQnyUDSHqGLdTF8RMmlwyaECTKWDnmhClMLV8In9";

            // See documentation on defining a message payload.
            var message = new Message()
            {
                Data = new Dictionary<string, string>()
                {
                    {"myData", "One more try to say Succeded!"},
                    
                },
                Token = registrationToken,
                //Topic = "all",   
                Notification = new Notification()
                {
                    Title = "Test from code",
                    Body = "Here is your test!"
                }
            };

            // Send a message to the device corresponding to the provided
            // registration token.
            string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
            // Response is a message ID string.
            Console.WriteLine("Successfully sent message: " + response);
        }
    }
}

Receiving and opening notifications with the app in the background increments the number of Data sent.

In addition the notification is not showing when the app is killed or when it is in the foreground.

Below is my code, the source code is from Gerald Versluis https://github.com/jfversluis/XFFCMPushNotificationsSample

using System;
using Xamarin.Forms;
using Plugin.FirebasePushNotification;
using Xamarin.Forms.Xaml;

namespace FCMTest
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();

            CrossFirebasePushNotification.Current.Subscribe("all");
            CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;
            CrossFirebasePushNotification.Current.OnNotificationReceived += Current_OnNotificationReceived;
            CrossFirebasePushNotification.Current.OnNotificationOpened += Current_OnNotificationOpened;
           //CrossFirebasePushNotification.Current.OnNotificationAction += Current_OnNotificationAction;
        }

        private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(
quot;Token from function OnTokenRefresh: {e.Token}");
        }

        private void Current_OnNotificationReceived(object source, FirebasePushNotificationDataEventArgs d)
        {
            System.Diagnostics.Debug.WriteLine("Received");
            foreach (var data in d.Data)
            {
                System.Diagnostics.Debug.WriteLine(
quot;{data.Key} : {data.Value}");
            }
        }

        /*private void Current_OnNotificationAction(object source, FirebasePushNotificationResponseEventArgs d)
        {
            System.Diagnostics.Debug.WriteLine("Opened");
            foreach (var data in d.Data)
            {
                System.Diagnostics.Debug.WriteLine(
quot;{data.Key} : {data.Value}");
            }
        }*/

        private void Current_OnNotificationOpened(object source, FirebasePushNotificationResponseEventArgs d)
        {
            System.Diagnostics.Debug.WriteLine("Opened");
            foreach (var data in d.Data)
            {
                System.Diagnostics.Debug.WriteLine(
quot;{data.Key} : {data.Value}");
            }
        }
        
        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}
using FirebaseAdmin;
using FirebaseAdmin.Messaging;
using Google.Apis.Auth.OAuth2;
using System;
using System.Collections.Generic;


namespace FCMDispatcher
{
    class Program
    {
        static void Main(string[] args)
        {
            FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromFile("fcm-test-98fe5-firebase-adminsdk-oknwc-c290af4ff0.json")
            });

            // This registration token comes from the client FCM SDKs.
            var registrationToken = "d8yECxIhJXQ:APA91bFGXSrnUVcP07TlN4HpvfstWwEdPQaj4wr6Z3Q-7JqcJJjrFy9LkWdlfzcCNDrZeVy55IDTWxvp5Gfyv8318uRRmIPo6Gp2IQnyUDSHqGLdTF8RMmlwyaECTKWDnmhClMLV8In9";

            // See documentation on defining a message payload.
            var message = new Message()
            {
                Data = new Dictionary<string, string>()
                {
                    {"myData", "One more try to say Succeded!"},
                    
                },
                Token = registrationToken,
                //Topic = "all",   
                Notification = new Notification()
                {
                    Title = "Test from code",
                    Body = "Here is your test!"
                }
            };

            // Send a message to the device corresponding to the provided
            // registration token.
            string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
            // Response is a message ID string.
            Console.WriteLine("Successfully sent message: " + response);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-02-17 08:53:39

尝试在数据有效载荷中添加高或最大的优先级。

Data = new Dictionary<string, string>()
{
    {"myData", "One more try to say Succeded!"},
    {"priority", "high"},                   
},

这种复杂条件必须匹配以显示您的通知:
https://github.com/crossgeeks/ FireBasePushNotificationPlugin/blob/d86266a9f45687b418f5f1e69c34868681d1ff6e27/plugin.firebasepushnotification/defaultpushnotificationhandificationhandler.android.android.android.cs#l151#l151

Try adding priority with high or max to your Data Payload.

Data = new Dictionary<string, string>()
{
    {"myData", "One more try to say Succeded!"},
    {"priority", "high"},                   
},

This complex condition must match to show your notification:
https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/d86266a9f45687b418f5f1e69c348681d1ff6e27/Plugin.FirebasePushNotification/DefaultPushNotificationHandler.android.cs#L151

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文