2

i am trying to understand how an android app on a device could send a notification to another android app on another device when a user adds a record to sql server database or deletes one. i started my research and figured out that i must use FCM. i applied this tutorial https://learn.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows and it worked but it didn't help me understand how the android app well send the notification. i tried to apply codes from java like this one https://blog.heyday.xyz/send-device-to-device-push-notifications-without-server-side-code-238611c143, i tried to convert it to c# but it didn't work for me. i actually didn't know how to completely write it as c#. i also tried this one: Xamarin android FCM Notification Client to client(phone to phone) but in vain. i searched a lot but i don't know why it feels complicated. if anyone could help me find a tutorial to do so. thanks in advance.

rana hd
  • 355
  • 3
  • 18
  • As far as I know, [FireBase Clound Message](https://learn.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows), just display remote notification that you send from FireBase Console, don't implement function that communicate with two Android app on different device. – Cherry Bu - MSFT Aug 23 '21 at 02:39
  • i really don't know, do you have any idea how to do so? – rana hd Aug 23 '21 at 07:55
  • Maybe you can use [Azure Notification hub to send notification](https://learn.microsoft.com/en-us/azure/developer/mobile-apps/notification-hubs-backend-service-xamarin-forms), and using web api to check sql server database changed. – Cherry Bu - MSFT Aug 26 '21 at 05:38

1 Answers1

1

To send notifications from one device to another in Xamarin.android there are several ways:

  1. using firebase cloud messaging when add new row to database Example: //process that add new row //post on FCM note: receive device must be register device token in firebase cloud messaging for more details: https://learn.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/firebase-cloud-messaging

  2. using task work in background in reciver device and check the number of row in database example:

    Task.Run(()=>{ //code that check number of row });

if changed call this method :

   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();
            }
        }