Cordova插件Firebasex在Android 12和13上未触发回调。

huangapple go评论70阅读模式
英文:

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) =&gt; {
        console.log(&quot;check permissions&quot;);
        window.FirebasePlugin.hasPermission(function(hasPermission) {
            console.log(&quot;has permission: &quot; + hasPermission);  
            if (hasPermission) {
                console.log(&quot;Remote notifications permission granted&quot;);
                // Granted
                window.FirebasePlugin.getToken(function(token){
                    console.log(&quot;Got FCM token: &quot; + token)
                }, function(error) {
                    console.log(&quot;Failed to get FCM token&quot;, error);
                });
            } else if (!requested) {
                // Request permission
                console.log(&quot;Requesting remote notifications permission&quot;);
                window.FirebasePlugin.grantPermission(function(hasPermission){
                    console.log(&quot;Permission was &quot; + (hasPermission ? &quot;granted&quot; : &quot;denied&quot;));
                });
            } else {
                // Denied
                console.log(&quot;Notifications won&#39;t be shown as permission is denied&quot;);
            }
        })
        window.FirebasePlugin.onMessageReceived(function(message){
            if(message &amp;&amp; message.url){
                history.push(message.url)
            }
        }, function(error){
            console.log(&quot;Error on message received &quot; + 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:

&lt;config-file target=&quot;AndroidManifest.xml&quot; parent=&quot;/*&quot;&gt;
   &lt;uses-permission android:name=&quot;android.permission.POST_NOTIFICATIONS&quot; /&gt;
&lt;/config-file&gt;

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.

&lt;preference name=&quot;Hostname&quot; value=&quot;localhost&quot; /&gt;

huangapple
  • 本文由 发表于 2023年7月10日 20:28:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定