Xamarin Webview 中的 Notification.permission

问题描述

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

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

这很好用,然后我可以使用

接收通知
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();

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

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

有什么想法吗?

解决方法

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

Notifications suppor matrix

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

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

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