Communication between DidReceiveNotificationResponse in AppDelegate.cs to ViewController on click of a notification

74 Views Asked by At

I am creating notifications like this when the user enters a geo fence.

UNMutableNotificationContent notificationContent = new UNMutableNotificationContent();
notificationContent.Title = "Entered region";
notificationContent.Body = "You have entered region " + fence.title + ". Touch here to check-in.";
RegisterNotification(notificationContent, "LocationStatus");


public static void RegisterNotification(UNMutableNotificationContent notificationContent, string identifier)
{
    UNUserNotificationCenter center = UNUserNotificationCenter.Current;
    notificationContent.Sound = UNNotificationSound.Default;
    UNNotificationRequest request = UNNotificationRequest.FromIdentifier(identifier, notificationContent, null);
    center.AddNotificationRequest(request, (NSError obj) =>
    {
    });
}

Now when the user clicks the notification I want to communicate to the ViewController and send the Fence object to the ViewController so the user can check in to that location.

Am not sure how to do this in iOS. In android I used Intent and Extras to do the same.

Android version:

string message = "You have entered " + k.title + ". Touch here to check-in.";
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);

if (action != null)
{
    intent.PutExtra("fence", Newtonsoft.Json.JsonConvert.SerializeObject(new FenceAction()
    {
        action = action,
        publicid = publicid
    }));
}

var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);

Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
    .SetSmallIcon(Resource.Drawable.notification)
    .SetContentTitle("Smart Office")
    .SetContentText(message)
    .SetAutoCancel(true)
    .SetContentIntent(pendingIntent)
    .SetOngoing(false);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(NOTIFICATION_AlARM_ID, notificationBuilder.Build());


protected override void OnNewIntent(Intent e)
{
    base.OnNewIntent(e);
if (e.Extras != null)
{
    string fence = e.Extras.GetString("fence");
    if (!string.IsNullOrEmpty(fence))
    {
        LocationService.FenceAction action = Newtonsoft.Json.JsonConvert.DeserializeObject<LocationService.FenceAction>(fence);
...
    }
    }
    }

I want to do the same thing with iOS. But not sure how to implement it. Thanks in advance.

1

There are 1 best solutions below

1
Gith On

I did that with NSNotificationCenter.DefaultCenter.PostNotificationName as below.

public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
        NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
        NSNotificationCenter.DefaultCenter.PostNotificationName("UpdateLocationChange", null, userInfo);
    }

And I added a observer in ViewDidLoad of the viewcontroller.

notificationToken = NSNotificationCenter.DefaultCenter.AddObserver((NSString)"UpdateLocationChange", UpdateLocationChange);