如何在 Xamarin 中拦截到 iOs 移动端点的 AWS SNS 消息?
How to intercept AWS SNS messages to iOs mobile endpoints in Xamarin?
关于在 Xamarin iOS 项目中使用 SNS 的 AWS -SNS 文档显示了如何注册一个 iOS 设备以从 SNS 接收消息,但不清楚如何在应用程序并以编程方式响应消息。如何捕获传入消息并进行适当处理,而不是仅显示收到的消息文本?这是通过发送与 AWS 控制台中显示的消息不同的消息来完成的吗?我可以在我的应用程序中的什么地方拦截它?
这是我一直关注的示例:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
// do something
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound,
null
);
app.RegisterUserNotifications(pushSettings);
app.RegisterForRemoteNotifications();
// do something
return true;
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData token) {
var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", "");
if (!string.IsNullOrEmpty(deviceToken)) {
//register with SNS to create an endpoint ARN
var response = await SnsClient.CreatePlatformEndpointAsync(
new CreatePlatformEndpointRequest {
Token = deviceToken,
PlatformApplicationArn = "YourPlatformArn" /* insert your platform application ARN here */
});
}
}
这是我要发送的消息:
{
"APNS_SANDBOX":"{\"aps\":{\"alert\":\"This is my message\"}}"
}
无论应用程序是否 运行,这似乎都可以很好地显示从 AWS 控制台发送的文本消息,但这不是我的应用程序所需要的。 (例如国际象棋应用程序,其中 SNS 消息用于交换一对用户的棋步,然后应用程序显示它们。)
FinishedLaunching 方法包含几个不是完全有用的 "do something" ,但我不知道该怎么做,比如在收到特定消息并传递内容时调用 PCL 中的某些方法该方法的消息。
您可以在 AppDelegate.cs
中订阅 DidReceiveRemoteNotification()
活动以获取您在 SNS 上发送的内容。
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// retrieve something from a server somewhere
}
当用户点击通知打开应用程序时,以及当该应用程序处于后台状态或前台状态时,都会触发此事件。
如果这个app关闭了这个事件不会触发,但是我们也可以通过参数options
.
获取public override bool FinishedLaunching(UIApplication app, NSDictionary options)
里面的内容
此外,如果您想在PCL中获取它,我们可以制作一个MessagingCenter
来实现:
在原生平台发送内容:
MessagingCenter.Send<object, NSDictionary>(this, "Notification", userInfo);
然后在 PCL 上你喜欢的地方接收此 MessagingCenter
:
MessagingCenter.Subscribe<object, NSDictionary>(this, "Notification", (sender, dic) =>
{
});
关于在 Xamarin iOS 项目中使用 SNS 的 AWS -SNS 文档显示了如何注册一个 iOS 设备以从 SNS 接收消息,但不清楚如何在应用程序并以编程方式响应消息。如何捕获传入消息并进行适当处理,而不是仅显示收到的消息文本?这是通过发送与 AWS 控制台中显示的消息不同的消息来完成的吗?我可以在我的应用程序中的什么地方拦截它?
这是我一直关注的示例:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
// do something
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound,
null
);
app.RegisterUserNotifications(pushSettings);
app.RegisterForRemoteNotifications();
// do something
return true;
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData token) {
var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", "");
if (!string.IsNullOrEmpty(deviceToken)) {
//register with SNS to create an endpoint ARN
var response = await SnsClient.CreatePlatformEndpointAsync(
new CreatePlatformEndpointRequest {
Token = deviceToken,
PlatformApplicationArn = "YourPlatformArn" /* insert your platform application ARN here */
});
}
}
这是我要发送的消息:
{
"APNS_SANDBOX":"{\"aps\":{\"alert\":\"This is my message\"}}"
}
无论应用程序是否 运行,这似乎都可以很好地显示从 AWS 控制台发送的文本消息,但这不是我的应用程序所需要的。 (例如国际象棋应用程序,其中 SNS 消息用于交换一对用户的棋步,然后应用程序显示它们。)
FinishedLaunching 方法包含几个不是完全有用的 "do something" ,但我不知道该怎么做,比如在收到特定消息并传递内容时调用 PCL 中的某些方法该方法的消息。
您可以在 AppDelegate.cs
中订阅 DidReceiveRemoteNotification()
活动以获取您在 SNS 上发送的内容。
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// retrieve something from a server somewhere
}
当用户点击通知打开应用程序时,以及当该应用程序处于后台状态或前台状态时,都会触发此事件。
如果这个app关闭了这个事件不会触发,但是我们也可以通过参数options
.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
里面的内容
此外,如果您想在PCL中获取它,我们可以制作一个MessagingCenter
来实现:
在原生平台发送内容:
MessagingCenter.Send<object, NSDictionary>(this, "Notification", userInfo);
然后在 PCL 上你喜欢的地方接收此 MessagingCenter
:
MessagingCenter.Subscribe<object, NSDictionary>(this, "Notification", (sender, dic) =>
{
});