【问题标题】:Onesignal NotificationOpened doesn't work in ios appOnesignal NotificationOpened 在 ios 应用程序中不起作用
【发布时间】:2018-01-12 14:10:39
【问题描述】:

我尝试在我的 ios 应用程序中使用 Onesignal 通知,该应用程序是在 Visual Studio 中使用 Xamarin 创建的。我想让应用程序在用户点击通知时打开 ViewController,但它不起作用。当用户点击通知时,应用程序打开 ViewController,当用户应用程序进入后台时它处于活动状态。 希望有人显示,我犯错的地方 代码如下:

public class AppDelegate : UIApplicationDelegate
{
    public static UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);
    public override UIWindow Window {
        get;
        set;
    }

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        OneSignal.Current.StartInit("mykey")
            .InFocusDisplaying(OSInFocusDisplayOption.None)
            .HandleNotificationOpened(HandleNotificationOpened)
            .EndInit();
        OneSignal.Current.IdsAvailable(IdsAvailable);            
        return true;
    }
    void IdsAvailable(string userID, string pushToken)
    {
        GlobalUserInfo._token = userID;
    }
    private static void HandleNotificationOpened(OSNotificationOpenedResult result)
    {
        OSNotificationPayload payload = result.notification.payload;
        Dictionary<string, object> additionalData = payload.additionalData;

        if (additionalData != null)
        {
            UINavigationController NavigationController = (UINavigationController)Storyboard.InstantiateInitialViewController();
            NotificationsViewController notificationsController = Storyboard.InstantiateViewController("NotificationsViewController") as NotificationsViewController;
            NavigationController.PushViewController(notificationsController, true);
        }
    }
}

NotificationsViewController 是用户收到的所有通知的简单列表。

更新 我注意到,函数 HandleNotificationOpened 在 iOS 中不起作用。

更新 发现错误。我忘了,我在其他地方使用 OneSignal Init。当我删除重复项时,HandleNotificationOpened 开始正常工作。

【问题讨论】:

    标签: ios visual-studio xamarin onesignal


    【解决方案1】:

    您应该找到当前根 ViewController 而不是创建新实例。所以请像这样修改:

    if (additionalData != null)
    {
        UINavigationController NavigationController = (UINavigationController)Window.RootViewController;
        NotificationsViewController notificationsController = Storyboard.InstantiateViewController("NotificationsViewController") as NotificationsViewController;
        NavigationController.PushViewController(notificationsController, true);
    }
    

    另外,如果根ViewController是一个UITabBarController,请出示这个视图控制器(NotificationsViewController)。

    【讨论】:

    • 谢谢,但我不能使用 Window,因为 HandleNotificationOpened 必须是静态的。
    • @AdeptusMechanicus 然后尝试通过UIApplication.SharedApplication.KeyWindow获取Window
    • 谢谢,Visual Studio 接受了这一点,但正如我在帖子的“更新”中所写,看起来“HandleNotificationOpened”函数本身不起作用。我在项目中添加了一个全局变量,并在“HandleNotificationOpened”中更改了它的值。但是应用程序显示,在用户点击推送通知后,这个全局并没有改变。
    • 好吧,我用“HandleNotificationOpened”函数解决了这个问题,一切正常。感谢您的帮助
    • @AdeptusMechanicus 好的,很高兴你解决了它。我之前没用过Onesignal。我们经常使用DidReceiveRemoteNotification(),当我们点击通知时它会触发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2013-10-11
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多