OneSignal Xamarin.Forms HandleNotificationOpened 重定向到通知特定页面

OneSignal Xamarin.Forms HandleNotificationOpened redirect to notification specific page

在我的 Xamarin.Forms 项目中,我使用 OneSignal 进行通知。在 iOS Xamarin.Forms.Application.Current.MainPage = new NavigationPage(new NotificationPage()); 中有效,但在 Android 中无效。我尝试使用消息中心与 PCL 项目进行通信。它在应用程序处于后台时有效,但在应用程序关闭时不工作。如何在 Android 收到通知时重定向通知特定页面?谢谢

注意:代码已编辑并已解决,我使用共享首选项来控制应用程序是否从通知启动。然后我加载 xamarin.Forms 应用程序。

您可以在调用 LoadApplication() 方法之前使用以下方法。

if (Intent.Extras != null)
        {
            foreach (var key in Intent.Extras.KeySet())
            {
                if (key != null)
                {
                    var value = Intent.Extras.GetString(key);                        
                    Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
                }
            }
        }

 LoadApplication(new App());

您必须在 OnMessageReceivedMethod() 中设置 intent.putExtra() 方法。

intent.PutExtra("Key", "value");

然后就可以根据这个键值在App.xaml.cs文件中使用重定向了。因为在 android 当应用重新初始化时打开通知。

我认为你不需要低于条件。

 if (extrasList[0] == "true")
    {
        LoadApplication(new App(true));

    }
    else
    {
        LoadApplication(new App(false));

    }

首先在全局级别存储您的值,以便您可以在 App.cs 文件中使用它。只需使用下面的代码来处理 App() class 中的页面导航。

 if (Device.RuntimePlatform == Device.Android)
                {
                    if (YourKey == "true")
                    {
                       //handle that page navigation
                    }
                    else
                    {
                       //Default Page of App
                    }
                }

注意:代码已编辑并已解决问题,我使用共享首选项来控制应用程序是否从通知启动。然后我加载 xamarin.Forms 应用程序

public class MainActivity : 
global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{



 protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        InitializeUI();

        global::Xamarin.Forms.Forms.Init(this, bundle);
        global::Xamarin.FormsMaps.Init(this, bundle);

        ImageCircleRenderer.Init();
        tV = new TextView(this);
        resources = this.Resources;


        OneSignal.Current.StartInit("***APP ID***")
           .InFocusDisplaying(OSInFocusDisplayOption.Notification)
           .HandleNotificationReceived(HandleNotificationReceived)
            .HandleNotificationOpened(HandleNotificationOpened)
             .EndInit();


        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        var LaunchFromNotification = prefs.GetString("is_notification_received", "false");
        if (LaunchFromNotification == "true")
        {
            LoadApplication(new App(true));

        }
        else
        {
            LoadApplication(new App(false));

        }

        OneSignal.Current.IdsAvailable(IdsAvailable); //Lets you retrieve the OneSignal player id and push token.
    }

}
        private static void HandleNotificationOpened(OSNotificationOpenedResult result)
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
        ISharedPreferencesEditor editor = prefs.Edit();
        editor.PutString("is_notification_received", "true");
        editor.Apply();

    }
protected override void OnResume()
       {
            base.OnResume();
            ISharedPreferences prefs = 
     PreferenceManager.GetDefaultSharedPreferences(this);

        ISharedPreferencesEditor editor = prefs.Edit();
        editor.Remove("is_notification_received");
        editor.PutString("is_notification_received", "false");
        editor.Apply();
    }