【问题标题】:HandleNotificationOpened are not handled if app is closed before i open the notification如果在我打开通知之前关闭应用程序,则不处理 HandleNotificationOpened
【发布时间】:2019-12-30 17:31:53
【问题描述】:

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

我尝试使用 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");
    }
}

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

【问题讨论】:

    标签: c# xamarin.forms cross-platform onesignal


    【解决方案1】:

    应用关闭时不会调用此方法。

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

    所以HandleNotificationOpened方法不会被调用。

    【讨论】:

    • 那么当app关闭时我如何处理后台数据?
    • @GuilhermeNimer 您好,应用关闭时无法处理远程通知。
    • 应用关闭时无法获取应用启动通知传递的后台数据?
    • @GuilhermeNimer 我认为这是不可能的。看看这个。firebase.google.com/docs/cloud-messaging/android/receive
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多