推送通知使用有效负载打开正确的页面

推送通知使用有效负载打开正确的页面,第1张

概述首先,我将XamarinForms用于WP8,iOS和Android应用程序.Goal:Iwanttogotoaspecificpagewhenthetoastisclickeddependinguponthepayloadinformationofthetoastnotification.我使用AzureNotificationHubs进行了推送通知,所有设置均正常运行.我使用MVV

首先,我将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                 }
总结

以上是内存溢出为你收集整理的推送通知使用有效负载打开正确的页面全部内容,希望文章能够帮你解决推送通知使用有效负载打开正确的页面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1079352.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存