【问题标题】:How to handle the tap events in Push Notifications in Xamarin Forms?如何处理 Xamarin Forms 中推送通知中的点击事件?
【发布时间】:2016-10-13 09:00:56
【问题描述】:

我创建了 Xamarin Forms PCL 项目。我已经集成了 OneSignal 推送通知,这些通知即将到来并显示良好。我想在有人点击通知时处理该事件,以便我可以显示与该通知对应的活动。我还添加了 Xam.Plugin.Pushnotifications 但我什至无法处理水龙头。消息来了,我可以捕获消息并使用它,但我想处理通知上的点击事件。我应该如何在 PCL 中做到这一点?

【问题讨论】:

    标签: android push-notification google-cloud-messaging xamarin.forms onesignal


    【解决方案1】:

    Xamarin OneSignal SDK 不支持 PCL 项目,因为它需要特定于平台的类。您需要将代码添加到共享项目中,如Xamarin OneSignal setup guide 所示。

    使用以下代码处理通知打开事件。

     // Notification Opened Delegate
     OneSignal.NotificationOpened exampleNotificationOpenedDelegate = delegate (OSNotificationOpenedResult result)
     {
        try
        {
           System.Console.WriteLine("OneSignal Notification opened:\nMessage: {0}", result.notification.payload.body);
           Dictionary<string, object> additionalData = result.notification.payload.additionalData;
           if (additionalData.Count > 0)
              System.Console.WriteLine("additionalData: {0}", additionalData);
    
    
           List<Dictionary<string, object>> actionButtons = result.notification.payload.actionButtons;
           if (actionButtons.Count > 0)
              System.Console.WriteLine("actionButtons: {0}", actionButtons);
        }
        catch (System.Exception e)
        {
           System.Console.WriteLine(e.StackTrace);
        }
     };
    
    OneSignal.StartInit("YOUR_APP_ID", "YOUR_GOOGLE_PROJECT_NUMBER")
            .HandleNotificationOpened(exampleNotificationOpenedDelegate)
            .EndInit();
    

    【讨论】:

    • 你说得对,我需要做平台特定的工作。谢谢:)
    • 他们现在支持 PCL。指南已更新:documentation.onesignal.com/docs/…
    • 请建议在iOS中在哪里编写此代码..我正在AppDelegate的完成启动方法中处理它,但是当我点击通知时没有任何反应。在我在 MainActivity 中编写代码的 Android 项目中一切正常。伙计们请指导我的ios。
    猜你喜欢
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多