Home > Blockchain >  Is there any way to retrieve notifactions received in my phone and display them on my c# application
Is there any way to retrieve notifactions received in my phone and display them on my c# application

Time:05-25

Hey guys I hope you are all doing well I have work to do, I'm working on a mobile application in C#, and I want to receive notifications received on my phone and display them in my application

here is a demonstration image : enter image description here

CodePudding user response:

If you want to develop your own notification service, I recommend you to use SignalR, subscribe to notifications in Xamarin, and use custom Popup to display content.

CodePudding user response:

From my perspective,you could use local Notifications to achieve this function.

1.Create a cross-platform interface

public interface INotificationManager
{
    event EventHandler NotificationReceived;
    void Initialize();
    void SendNotification(string title, string message, DateTime? notifyTime = null);
    void ReceiveNotification(string title, string message);
}

2.For iOS,create the iOS interface implementation,below is the Pseudo code, for more details, please refer to the sample code.

namespace LocalNotifications.iOS
{
    public class iOSNotificationManager : INotificationManager
    { 
        public void Initialize()
        {
            // request the permission to use local notifications
            UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
            {
                hasNotificationsPermission = approved;
            });
        }

        public void SendNotification(string title, string message, DateTime? notifyTime = null)
        {
            // EARLY OUT: app doesn't have permissions
            if (!hasNotificationsPermission)
            {
                return;
            }

            UNNotificationTrigger trigger;
            if (notifyTime != null)
            {
                // Create a calendar-based trigger.
                trigger = UNCalendarNotificationTrigger.CreateTrigger(GetNSDateComponents(notifyTime.Value), false);
            }
            else
            {
                // Create a time-based trigger, interval is in seconds and must be greater than 0.
                trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.25, false);
            }

        }
     
    }
}

3.And then Handle incoming notifications on iOS.

public class iOSNotificationReceiver : UNUserNotificationCenterDelegate
{
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
     
    }

    void ProcessNotification(UNNotification notification)
    {
     //uses the DependencyService to get an instance of the iOSNotificationManager class
    }    
}

4.Last but not least,The AppDelegate class must specify an iOSNotificationReceiver object as the UNUserNotificationCenter delegate during application startup.

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();

    UNUserNotificationCenter.Current.Delegate = new iOSNotificationReceiver();

    LoadApplication(new App());
    return base.FinishedLaunching(app, options);
}
  • Related