首先,我将Xamarin Forms用于WP8,iOS和Android应用程序.
Goal:
I want to go to a specific page when the toast is clicked depending
upon the payload information of the toast notification.
我使用Azure Notification Hubs进行了推送通知,所有设置均正常运行.我使用MVVMLight及其依赖项注入来专门为每个平台设置推送通知.
由于所需的格式不同,每个有效载荷都需要发送一些不同的信息.对于每一个,您都会注意到我想在有效负载中发送SignalId,以根据常规推送通知在接收设备上执行所需的不同操作.
安卓系统
{
"data" : {
"msg" : "message in here",
"signalId" : "id-in-here",
},
}
的iOS
{
"aps" : { "alert" : "message in here" },
"signalId" : "id-in-here"
}
Windows Phone 8
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
<wp:Toast>
<wp:Text1>category</wp:Text1>
<wp:Text2>message in here</wp:Text2>
<wp:Param>?signalId=id-in-here</wp:Param>
</wp:Toast>
</wp:Notification>
.
Question:
How do I get this information in a Xamarin Forms app and redirect to
the appropriate page when the application is reactivated because the
user clicked on the toast notification?
我想在应用程序加载时获取有效负载信息,然后说,是的,其中包含SignalId,让我们重定向到此页面.
目前,它会执行所有操作,并在单击烤面包通知时显示该应用程序.我必须针对该应用程序执行此操作,还是可以使用Xamarin Forms方式?
即使您只知道如何在一个平台上进行操作,也应感谢您的帮助,我可能可以从那里在其他平台上工作.
解决方法:
我已经找到了在所有平台上执行此操作的方法. Windows已经过测试,Android和iOS尚未经过测试.
如果应用程序在后台,则Windows和iOS会在show toast通知上工作,如果应用程序在前台,则让您的代码处理它.不论应用程序状态如何,Android都会显示吐司.
使用Windows Phone 8,我需要转到MainPage.xaml.cs并添加此替代项.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (this.NavigationContext.QueryString.ContainsKey("signalId"))
{
var signalId = this.NavigationContext.QueryString["signalId"];
var id = Guid.Empty;
if (signalId != null
&& Guid.TryParse(signalId, out id)
&& id != Guid.Empty)
{
this.NavigationContext.QueryString.Clear();
Deployment.Current.dispatcher.BeginInvoke(() =>
{
// Do my navigation to a new page
});
}
}
}
对于GcmService中的Android
protected override void OnMessage(Context context, Intent intent)
{
Log.Info(Tag, "GCM Message Received!");
var message = intent.Extras.Get("msg").ToString();
var signalId = Guid.Empty;
if (intent.Extras.ContainsKey("signalId"))
{
signalId = new Guid(intent.Extras.Get("signalId").ToString());
}
// Show notification as usual
CreateNotification("", message, signalId);
}
然后在CreateNotification函数中,在Intent中添加一些额外的信息.
var uiIntent = new Intent(this, typeof(MainActivity));
if (signalId != Guid.Empty)
{
uiIntent.PutExtra("SignalId", signalId.ToString());
}
然后在MainActivity.cs中覆盖此功能
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (data.HasExtra("SignalId"))
{
Guid signalId = new Guid(data.GetStringExtra("SignalId"));
if (signalId != Guid.Empty)
{
data.RemoveExtra("SignalId");
// Do you navigation
}
}
}
在iOS中,您会注意到我已经增强了默认的ProcessNotification()
void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
{
// Check to see if the dictionary has the aps key. This is the notification payload you would have sent
if (null != options && options.ContainsKey(new Nsstring("aps")))
{
//Get the aps dictionary
var aps = options.ObjectForKey(new Nsstring("aps")) as NSDictionary;
var alert = string.Empty;
//Extract the alert text
// NOTE: If you're using the simple alert by just specifying
// " aps:{alert:"alert msg here"} " this will work fine.
// But if you're using a complex alert with Localization keys, etc.,
// your "alert" object from the aps dictionary will be another NSDictionary.
// Basically the json gets dumped right into a NSDictionary,
// so keep that in mind.
if (aps.ContainsKey(new Nsstring("alert")))
alert = ((Nsstring) aps[new Nsstring("alert")]).ToString();
// If this came from the ReceivedRemoteNotification while the app was running,
// we of course need to manually process things like the sound, badge, and alert.
if (!fromFinishedLaunching)
{
//Manually show an alert
if (!string.IsNullOrEmpty(alert))
{
var signalId = new Guid(options.ObjectForKey(new Nsstring("signalId")) as Nsstring);
// Show my own toast with the signalId
}
}
}
}
然后在FinishedLaunching函数中检查是否有任何有效负载
// Check if any payload from the push notification
if (options.ContainsKey("signalId"))
{
var signalId = new Guid(options.ObjectForKey(new Nsstring("signalId")) as Nsstring);
// Do the navigation here
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。