首页 > 解决方案 > Xamarin Webview 中的 Notification.permission

问题描述

我对 Xamarin 应用程序开发很陌生。我有一个使用 FirebaseNotifications 的网络应用程序。在 webapp 中,我使用这个 javascript 代码来检查是否接受了通知。

 if (Notification.permission !== "granted") {
            Notification.requestPermission();
        }

这工作得很好,然后我可以使用接收通知

firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();

messaging.onMessage(function (payload) {
   //do something
}

但是,当我在 XamarinForms WebView 中调用同一个网站时,我得到一个异常,Notificaton即为空。因此我不能授予权限,也不能收到任何消息。

任何想法?

标签: androidfirebasexamarinwebviewnotifications

解决方案


如果您查看通知规范:https ://developer.mozilla.org/en-US/docs/Web/API/Notification/permission它在 Android WebView 和 iOS WebView 中不受支持,Xamarin.Forms 在幕后使用。此通知权限请求仅适用于 Web 浏览器。

通知支持矩阵

因此,此通知权限不是在移动应用程序中接收通知所必需的。对于要接收通知的移动应用程序,您需要不同类型的设置。您可以在这里阅读更多相关信息:https ://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows

这将涉及您必须从 NuGet 安装 GooglePlayServices 和 FirebaseMessaging,并为您的应用程序运行的每个平台配置它们。

如果您想在 WebApp 中调用某些特定 JavaScript 时调用一些本机代码来设置这些通知,请在此处查看有关为 WebView 创建自定义渲染器的文档:https ://docs.microsoft.com/en-us /xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview#create-the-custom-renderer-on-android


推荐阅读