单击通知时 AppDelegate.cs 中的 DidReceiveNotificationResponse 与 ViewController 之间的通信

问题描述 投票:0回答:1

当用户进入地理围栏时,我正在创建这样的通知。

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) =>
    {
    });
}

现在,当用户单击通知时,我想与 ViewController 通信并将 Fence 对象发送到 ViewController,以便用户可以签入该位置。

我不确定如何在 iOS 中执行此操作。在 android 中,我使用 Intent 和 Extras 来执行相同的操作。

安卓版本:

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

我想在 iOS 上做同样的事情。但不确定如何实施。预先感谢。

ios xamarin.ios uiviewcontroller notifications uilocalnotification
1个回答
0
投票

我使用 NSNotificationCenter.DefaultCenter.PostNotificationName 执行此操作,如下所示。

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

我在视图控制器的ViewDidLoad中添加了一个观察者。

notificationToken = NSNotificationCenter.DefaultCenter.AddObserver((NSString)"UpdateLocationChange", UpdateLocationChange);
© www.soinside.com 2019 - 2024. All rights reserved.