如果应用程序在我打开通知之前关闭,则不会处理 HandleNotificationOpened

HandleNotificationOpened are not handled if app is closed before i open the notification

HandleNotificationOpened 在应用程序在后台或 运行ning 时工作正常,但如果我在应用程序关闭时打开通知,它不会被触发。

我尝试使用 SecureStorage 保存来自事件的数据,因为我不确定事件 运行 是在错误的时间还是根本没有 运行。

public App()
{
    OneSignal.Current.StartInit("onesignal-id").HandleNotificationOpened(HandleNotificationOpened).HandleNotificationReceived(HandleNotificationReceived).EndInit();
}


private async void HandleNotificationOpened(OSNotificationOpenedResult result)
{
    var data = result.notification.payload.additionalData;

    if (data != null)
    {
        data.TryGetValue("Title", out object Title);
        data.TryGetValue("Conteudo", out object Conteudo);
        data.TryGetValue("Link", out object RLink);
        string lastvar = (Title.ToString().GetHashCode() + Conteudo.ToString().GetHashCode() + RLink.ToString().GetHashCode()).ToString();

        if (!ChecarDB(lastvar))
        {
            InserirDB(Title.ToString(), Conteudo.ToString(), RLink.ToString());
        }

        await SecureStorage.SetAsync("UrlFromPush", RLink.ToString());
        var page = new MainPage();
        MessagingCenter.Send<MainPage>(page, "MudarURL");
    }
}

预期结果是应用程序正确处理事件,完全没有错误消息。

应用关闭时不会调用该方法

虽然我没有使用OneSignal推送通知,但是根据Android/iOS系统通知处理机制,当应用关闭时,收到远程通知,点击通知会重启app,通知处理机制由系统托盘处理

所以HandleNotificationOpened方法不会被调用

我已经使用 URI Scheme 访问早期初始化后台数据解决了这个问题,将 Intent?.Data?.EncodedQuery;HandleNotificationOpened 方法替换为自定义格式化方法,这就是我得到预期结果的方式。