android - 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
即为空。因此我不能授予权限,也不能收到任何消息。
任何想法?
解决方案
如果您查看通知规范: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
推荐阅读
- php - 如何在 Yii2 中渲染中间内容?
- c++ - 从两点重新绘制一条线会产生稍微不准确的结果
- php - 如何在 CMB2“所见即所得”类型中启用 Wpbakery Page Builder(Visual Composer)?
- angular - Angular - 提交异步表单
- python - 密码学-RSA(python)中未定义 Default_backend
- python - python tkinter恢复没有标题栏的窗口
- java - OO:容器中是否包含自行车或椅子?
- raku - 找出容器是类还是对象
- c++ - 从 explorer.exe 读取 PCACLIENT 内存
- java - 是否可以在非 Android 应用程序中使用 Android 支持库?