Forum Xamarin Xamarin.Forms

How to implement backgrounding & local notifications in Xamarin.Forms iOS application ?

AbhiVazeAbhiVaze Member ✭✭

I am creating Xamarin.forms application. I want my application to hit a REST API after every 1 minute, get json data from there and show notification if new data is received. I want this to work on iOS platform.
Can anyone please guide me ?
I am new to iOS.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    If you want to achieve Local notifications in Xamarin.Forms, you can check the official document:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications

    And this document include a full sample of how to create a cross-platform abstraction to send and receive local notifications with Xamarin.Forms.You can check it.

    If you want to this work kn ios platform, you just need to Create the iOS interface implementation

    I want my application to hit a REST API after every 1 minute

    For this question,you can use a Timer to achieve this.
    You can check the official document: Timer Class

    A usage example:

    Device.StartTimer(TimeSpan.FromSeconds(1), () =>
    {
      Task.Run(async () =>
      {
        var data= await RequestJsonAsync();
        // do something with the data...(send notification)
        // other code
      });
      return true;
    });
    
  • AbhiVazeAbhiVaze Member ✭✭
    edited June 13

    I am getting error that type or namespace "NotificationEventArgs" could not be found.
    I added dll in iOS application and the error was gone.
    I am getting error at line : ShowNotification(evtData.Title, evtData.Message);

    Error CS1061: 'NSNotificationEventArgs' does not contain a definition for 'Title' and no accessible extension method 'Title' accepting a first argument of type 'NSNotificationEventArgs' could be found (are you missing a using directive or an assembly reference?) (CS1061) (iOSNotifications)

  • jezhjezh Member, Xamarin Team Xamurai

    According to your description, It is difficult to locate your problem.
    If it is convenient for you, could you please post a basic demo or some code snippet so that we can test on our side?

  • AbhiVazeAbhiVaze Member ✭✭

    @jezh said:
    According to your description, It is difficult to locate your problem.
    If it is convenient for you, could you please post a basic demo or some code snippet so that we can test on our side?

    Here's code sample. Getting error for Title & Message.
    Please find error in the attached screenshot.

    public MainPage()
    {
    InitializeComponent();

            notificationManager = DependencyService.Get<INotificationManager>();
            notificationManager.NotificationReceived += (sender, eventArgs) =>
            {
                var evtData = (NSNotificationEventArgs)eventArgs;
                ShowNotification(evtData.Title, evtData.Message);
            };
        }
    
  • jezhjezh Member, Xamarin Team Xamurai

    Hi @AbhiVaze , if it is convenient for you , could you please post a basic demo or more code snippets (not just a screenshot )so that we can try to reproduce this question on our side?

Sign In or Register to comment.