英文:
Cordova plugin firebasex not firing callback on Android 12 & 13
问题
我有一个使用cordova-plugin-firebasex管理移动端推送通知的Cordova应用程序。
我有以下实现来检查通知权限、授予权限和保存FCM令牌。对于IOS设备和Android 12之前的Android设备,它都正常工作。
checkNotificationPermissions = (requested) => {
console.log("check permissions");
window.FirebasePlugin.hasPermission(function(hasPermission) {
console.log("has permission: " + hasPermission);
if (hasPermission) {
console.log("Remote notifications permission granted");
// Granted
window.FirebasePlugin.getToken(function(token){
console.log("Got FCM token: " + token)
}, function(error) {
console.log("Failed to get FCM token", error);
});
} else if (!requested) {
// Request permission
console.log("Requesting remote notifications permission");
window.FirebasePlugin.grantPermission(function(hasPermission){
console.log("Permission was " + (hasPermission ? "granted" : "denied"));
});
} else {
// Denied
console.log("Notifications won't be shown as permission is denied");
}
})
window.FirebasePlugin.onMessageReceived(function(message){
if(message && message.url){
history.push(message.url)
}
}, function(error){
console.log("Error on message received " + error);
})
}
checkNotificationPermissions会被正确调用,而且window.FirebasePlugin会被正确初始化(如果我在函数顶部使用console.log打印它的话)。然而,在Android 13物理设备上运行时,代码似乎永远不会到达window.FirebasePlugin.hasPermission回调(无论成功还是出错)。
根据Android 13的新要求,我已经在我的config.xml中添加了POST_NOTIFICATIONS权限:
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
</config-file>
我正在运行:
- cordova 12.0.0(cordova-ios 6.3.0和cordova-android 12.0.0)
- cordova-plugin-firebasex 16.1.0
- cordova-plugin-statusbar 3.0.0
- 带有Android 13的Samsung Galaxy A33
有什么想法吗?感谢您的帮助。
编辑
当在Android 12和13上运行时,我在logcat中发现了这个日志,表明exec()函数不断被忽略。但是无法弄清楚为什么...
CordovaBridge: Ignoring exec() from previous page load.
应该显示以下日志:
- 检查权限
- has permission: false
- 请求远程通知权限
- 权限已被授予
我得到的日志是:
- 检查权限
英文:
I have a cordova application using cordova-plugin-firebasex to manage push notifications on mobile.
I have the following implementation to check about notification permissions, grant permissions and save FCM tokens. It works fine for IOS devices and Android devices prior of Android 12.
checkNotificationPermissions = (requested) => {
console.log("check permissions");
window.FirebasePlugin.hasPermission(function(hasPermission) {
console.log("has permission: " + hasPermission);
if (hasPermission) {
console.log("Remote notifications permission granted");
// Granted
window.FirebasePlugin.getToken(function(token){
console.log("Got FCM token: " + token)
}, function(error) {
console.log("Failed to get FCM token", error);
});
} else if (!requested) {
// Request permission
console.log("Requesting remote notifications permission");
window.FirebasePlugin.grantPermission(function(hasPermission){
console.log("Permission was " + (hasPermission ? "granted" : "denied"));
});
} else {
// Denied
console.log("Notifications won't be shown as permission is denied");
}
})
window.FirebasePlugin.onMessageReceived(function(message){
if(message && message.url){
history.push(message.url)
}
}, function(error){
console.log("Error on message received " + error);
})
}
The checkNotificationPermissions is properly called and window.FirebasePlugin is properly initialized (object exist if I console.log it at the top of the function). However, when running on a Android 13 physical device, code never seems to reach window.FirebasePlugin.hasPermission callback (neither success nor error).
I've already added POST_NOTIFICATIONS permission in my config.xml according to new Android 13 requirements:
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
</config-file>
I'm running on
- cordova 12.0.0 (cordova-ios 6.3.0 & cordova-android 12.0.0)
- cordova-plugin-firebasex 16.1.0
- cordova-plugin-statusbar 3.0.0
- Samsung Galaxy A33 with Android 13
Any idea ? Thanks for your help.
EDIT
I found this log in the logcat when running on Android 12 & 13 suggesting exec() function is constantly ignored. Can't figure out why though...
CordovaBridge: Ignoring exec() from previous page load.
Should display the following logs:
- check permissions
- has permission: false
- Requesting remote notifications permission
- Permission was granted
Logs I get:
- check permissions
答案1
得分: 0
我最终找到了错误的来源和解决方法。
忽略 exec() 错误:
CordovaBridge:忽略来自先前页面加载的 exec()。
源自另一个错误:
CordovaBridge:从受限制的来源调用了 gap_init:http://localhost/
根据这篇帖子 https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/600#issuecomment-771246329,我必须确保我将主机名和方案加入了白名单。我已经为方案做了,但将主机名添加到我的 config.xml 中解决了这个问题。
英文:
I end up finding the source of the error and the solution.
The ignoring exec() error:
CordovaBridge: Ignoring exec() from previous page load.
originated from another error:
CordovaBridge: gap_init called from restricted origin: http://localhost/
According to this post <https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/600#issuecomment-771246329>, I had to make sure I whitelisted my hostname and scheme. I already did it for the scheme but adding the hostname in my config.xml solved the problem.
<preference name="Hostname" value="localhost" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论