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

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

首先,我使用Xamarin Forms作为WP8,iOS和Android应用程序。

目标:

根据Toast通知的有效负载信息,我想在点击Toast时转到特定页面。

我使用Azure通知中心推送通知所有设置和运行良好。我使用MVVMLight及其依赖注入来专门为每个平台设置推送通知。

由于需要不同的格式,每个有效载荷需要稍微不同。每次你都会注意到我想在有效负载中发送一个SignalId,以便根据常规推送通知在接收设备上执行不同的操作。

Android的

{
  "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>

.

题:

如何重新激活应用程序,因为用户点击了Toast通知,我如何在Xamarin Forms应用程序中获取此信息并重定向到相应的页面?

我想在应用程序加载时获取有效负载信息,然后说,是的,这包含一个SignalId,让我们重定向到这个页面。

所有这一切都会在点击Toast通知时显示应用程序。我必须特定于应用程序,还是有Xamarin Forms方式?

任何帮助,即使你只知道如何在一个平台上做到这一点,我可以从那里开始绕过其他平台。

android ios windows-phone-8 push-notification xamarin.forms
1个回答
3
投票

我找到了为所有平台做这件事的方法。 Windows已经过测试,Android和iOS都没有。

如果应用程序在后台,Windows和iOS可以显示toast通知,或者如果应用程序位于前台,则让代码处理它。无论应用程序状态如何,Android都会显示Toast。

使用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         
        }
© www.soinside.com 2019 - 2024. All rights reserved.