【问题标题】:send push notification from one android app to another when data in sql server database changes当 sql server 数据库中的数据发生变化时,从一个 android 应用程序向另一个应用程序发送推送通知
【发布时间】:2021-08-22 11:33:53
【问题描述】:

我试图了解当用户向 sql server 数据库添加记录或删除记录时,设备上的 android 应用程序如何向另一台设备上的另一个 android 应用程序发送通知。我开始研究并发现我必须使用 FCM。我应用了本教程https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows 并且它有效,但它并没有帮助我理解 android 应用程序如何很好地发送通知。我试图从 java 中应用这样的代码https://blog.heyday.xyz/send-device-to-device-push-notifications-without-server-side-code-238611c143,我试图将其转换为 c#,但它对我不起作用。我实际上不知道如何将它完全写成 c#。我也试过这个:Xamarin android FCM Notification Client to client(phone to phone) 但徒劳无功。我搜索了很多,但我不知道为什么感觉很复杂。如果有人可以帮我找到一个教程来做到这一点。提前致谢。

【问题讨论】:

  • 据我所知,FireBase Clound Message,只是显示您从 FireBase 控制台发送的远程通知,不要实现与不同设备上的两个 Android 应用程序通信的功能。
  • 我真的不知道,你知道怎么做吗?
  • 也许你可以使用Azure Notification hub to send notification,并使用web api 来检查sql server 数据库的变化。

标签: c# push-notification xamarin.android firebase-cloud-messaging


【解决方案1】:

要在 Xamarin.android 中将通知从一台设备发送到另一台设备,有几种方法:

  1. 向数据库添加新行时使用 Firebase 云消息传递 例子: //添加新行的过程 //发布在 FCM 上 注意:接收设备必须在 firebase 云消息中注册设备令牌 更多细节: https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/firebase-cloud-messaging

  2. 在接收设备的后台使用任务工作并检查数据库中的行数 示例:

    Task.Run(()=>{ //检查行数的代码 });

如果改变调用这个方法:

   public void SendNotification(int periority_notification, return_user_info User_info, Context context)
        {
            /*here intent to open layout when click on notification*/
            Intent inten = new Intent(context, typeof(MainActivity));
            const int pendingIntentId = 0;

            PendingIntent pendingIntent =

                PendingIntent.GetActivity(context, pendingIntentId, inten, PendingIntentFlags.OneShot);
            var title = User_info.User_Email;
        var message = User_info.User_Name.Trim() + "   Reserved Request";
            using (var notificationManager = NotificationManager.FromContext(context))
            {
                Notification notification;
                if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
                {
                    notification = new Notification.Builder(context)
                                                         .SetContentIntent(pendingIntent)
                                                         .SetContentTitle(title)
                                                         .SetContentText(message)
                                                         .SetAutoCancel(true)
                                                         .SetPriority(1)
                                                          .SetSmallIcon(Resource.Drawable.carparts)
                                                         .SetDefaults(NotificationDefaults.All)
                                                         .Build();

                }
                else
                {
                    var myUrgentChannel = context.PackageName;
                    const string channelName = "SushiHangover Urgent";

                    NotificationChannel channel;
                    channel = notificationManager.GetNotificationChannel(myUrgentChannel);
                    if (channel == null)
                    {
                        channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
                        channel.EnableVibration(true);
                        channel.EnableLights(true);
                        channel.LockscreenVisibility = NotificationVisibility.Public;
                        notificationManager.CreateNotificationChannel(channel);
                    }
                    channel?.Dispose();

                    notification = new Notification.Builder(context)
                                                         .SetContentIntent(pendingIntent)
                                                         .SetChannelId(myUrgentChannel)
                                                         .SetContentTitle(title)
                                                         .SetContentText(message)
                                                         .SetAutoCancel(true)
                                                         .SetSmallIcon(Resource.Drawable.carparts)
                                                         .Build();
                }
                notificationManager.Notify(periority_notification, notification);
                notification.Dispose();
            }
        }

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 2018-01-20
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多