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

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

提问人:Gith 提问时间:9/25/2023 更新时间:9/28/2023 访问量:50

问:

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

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来做同样的事情。

Android版本:

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上做同样的事情。但不确定如何实现它。提前致谢。

Xamarin.iOS UIViewController 通知 UIProcessNotification

评论

0赞 Sean He-MSFT 9/27/2023
单击通知时,“WillPresentNotification”是否触发?如 Xamarin.Forms 中的 doc-Local 通知中所述,可以通过此“WillPresentNotification”方法在前台处理通知。您想要达到什么效果,以及要更新哪个控制器?
0赞 Gith 9/28/2023
是的,“WillPresentNotification”被触发。我找到了解决方案,并在下面给出了它。谢谢。

答:

0赞 Gith 9/28/2023 #1

我用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);

评论

0赞 Community 10/8/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。