HMS-Core / hms-cordova-plugin

This repo contains all of Cordova HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
100 stars 46 forks source link

Huawei Push Notification onMessageReceived not working #14

Closed mozhn closed 3 years ago

mozhn commented 3 years ago

Hello, first of all thank your for providing this repository. I have an app that starts the app when I get a data notification and then shows the call screen. The application needs to start via startActivity. Everything works as it should when I tested it with emulators included in AppGallery Developer. However, I found out that this feature was not working properly on a customer's device.

Customer device model: Huawei Mate 20 Pro

My HmsPushMessageService.java file

    @Override
    public void onMessageReceived(final RemoteMessage message) {
        Log.w(TAG, "** onMessageReceived **");

        hmsLogger = HMSLogger.getInstance(getApplicationContext());
        try {
            final boolean isApplicationInForeground = ApplicationUtils.isApplicationInForeground(
                getApplicationContext());
            if (isApplicationInForeground) {
                HmsMessagePublisher.sendMessageReceivedEvent(message);
                hmsLogger.sendPeriodicEvent("onMessageReceived");
            } else {
                final String myAppId = getApplicationContext().getApplicationInfo().uid + "";
                if (getApplication() == null) {
                    return;
                }
                final SharedPreferences sharedPref = getApplication().getApplicationContext()
                    .getSharedPreferences(getPackageName() + "." + myAppId, Context.MODE_PRIVATE);
                if (webView == null) {
                    webView = new WebView(getApplicationContext());
                    webView.setWebViewClient(new WebViewClient());
                    webView.getSettings().setSavePassword(false);
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.addJavascriptInterface(new BackgroundJavaScriptInterface(getApplication()),
                        "HmsLocalNotification");

                }
                String preFunction = sharedPref.getString("data", null);
                if (preFunction != null) {
                    preFunction = preFunction.replace("=>", "");
                }
                String function = String.format(Locale.ENGLISH, "function callback%s", preFunction);
                String s = "[\"HmsLocalNotification\"].backgroundLocalNotification";
                StringBuilder newFunction = new StringBuilder();
                if (function.contains("ionic")) {
                    final String[] lines = function.split("\n");
                    for (final String line : lines) {
                        if (line.contains(s)) {
                            final int start = line.indexOf("(");
                            final int end = line.indexOf(";");
                            final String param = line.substring(start, end);
                            newFunction.append("HmsLocalNotification.backgroundLocalNotification(")
                                .append(param)
                                .append(");");
                        } else {
                            newFunction.append(line);
                        }
                    }
                    function = newFunction.toString();
                }
                webView.evaluateJavascript(function, null);
                webView.evaluateJavascript(
                    String.format(Locale.ENGLISH, "callback(%s);", RemoteMessageUtils.fromMap(message)), null);

                if (message.getData() == null) {
                    return;
                }

                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                startActivity(intent);
            }
        } catch (final JSONException e) {
            Log.w(TAG, "onMessageReceived: " + e.getLocalizedMessage());
            hmsLogger.sendPeriodicEvent("onMessageReceived", e.getLocalizedMessage());
        }
    }
mozhn commented 3 years ago

I added the following code to understand exactly where I am experiencing the problem. Toast appears when the application is closed. In this case my startActivity code doesn't work. Is there anything that could cause this?

Toast.makeText(getApplicationContext(),"NOTIFICATION RECEIVED",Toast.LENGTH_LONG).show();

mesutgedik commented 3 years ago

Hi @mozhn , Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. I think your emulator API level is lower than 29. So that your startActivity method work successfully in emulator. But Mate 20 Pro has API level 29 and you can not start activity. You can examine this document : Activities Background Start

mozhn commented 3 years ago

I specifically checked the emulators I tested. It worked smoothly on the emulator with Android 10. In the model I mentioned, I cannot start an activity even if the application does not close completely. I wonder how apps like WhatsApp do this.