OneSignal doesn't open my flutter app on click [Android ONLY]

811

Solution 1

It's a problem from 3.2.0 to 3.2.3

Using 3.0.0 I solved the problem: onesignal_flutter: 3.0.0

To follow the issue: https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/462

Solution 2

Some Fixes Till Onesignal fix it. Ask for the user to enable { display popup window while running in background permission } in other permission of app settings.

Code For Button (Ask user to enable)

if (Platform.isAndroid) {
          try {
            const platformChannel = MethodChannel('getDeviceInfo');
            Map<dynamic, dynamic> result =
                await platformChannel.invokeMethod('getAndroidInfo');
            print(result);
            if(result.isNotEmpty){
              if(result["MANUFACTURER"].toString().toLowerCase() == "xiaomi" &&
                  result["IS_OS_MIUI"]==true){
                const platformChannel = MethodChannel('notiOpen');
                await platformChannel.invokeMethod('startNotiOpen');
              }
            }
          } on PlatformException catch (e) {
            if (kDebugMode) {
              print(e);
            }
          }
        }

#Code in main activity

package com.yourpackagename
import android.content.Intent
import android.content.pm.PackageInfo
import android.os.Build
import android.text.TextUtils
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.util.*

class MainActivity : FlutterActivity() {
    private val CHANNEL = "notiOpen"
    private val CHANNEL2 = "getDeviceInfo"

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(
        flutterEngine.dartExecutor.binaryMessenger,
        CHANNEL
    ).setMethodCallHandler { call, result ->
        if (call.method == "startNotiOpen") {
            notifiOpen()
        } else {
            result.notImplemented()
        }
    }

    MethodChannel(
        flutterEngine.dartExecutor.binaryMessenger,
        CHANNEL2
    ).setMethodCallHandler { call, result ->
        if (call.method == "getAndroidInfo") {
            val deviceInfo = getDeviceInfo()
            if (deviceInfo.isNotEmpty()) {
                result.success(deviceInfo)
            } else {
                result.error("UNAVAILABLE", "Not Recived Device Info", null)
            }
        } else {
            result.notImplemented()
        }
    }
}

private fun getDeviceInfo(): Map<String, Any> {
    var deviceInfo: Map<String, Any> = mapOf()
    try {
        val packageInfo: PackageInfo = applicationContext.getPackageManager()
            .getPackageInfo(applicationContext.getPackageName(), 0)
        deviceInfo = mapOf(
            "MANUFACTURER" to Build.MANUFACTURER,
            "BRAND" to Build.BRAND,
            "MODEL" to Build.MODEL,
            "BOARD" to Build.BOARD,
            "HARDWARE" to Build.HARDWARE,
            "BUILD_CODE" to Build.VERSION.SDK_INT,
            "BUILD_CODE" to BuildConfig.VERSION_CODE,
            "DEVICE" to Build.DEVICE,
            "ID" to Build.ID,
            "VERSION_NAME" to Build.VERSION.RELEASE,
            "TYPE" to Build.TYPE,
            "APP_VERSION" to packageInfo.versionName,
            "APP_BUILD_NUMBER" to packageInfo.longVersionCode,
            "IS_OS_MIUI" to !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"))
        )
        return deviceInfo
    } catch (e: Exception) {
    }
    return deviceInfo
}

private fun notifiOpen() {
    try {
        val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val manufacturer = Build.MANUFACTURER
        if ("xiaomi".equals(manufacturer, ignoreCase = true)) {
            intent.setClassName(
                "com.miui.securitycenter",
                "com.miui.permcenter.permissions.PermissionsEditorActivity"
            );
            intent.putExtra("extra_pkgname", "com.yourpackagename")
            startActivity(intent);
        }
    } catch (ignore: java.lang.Exception) {
    } catch (e: Exception) {
    }
    try {
        val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val manufacturer = Build.MANUFACTURER
        if ("xiaomi".equals(manufacturer, ignoreCase = true)) {
            intent.setClassName(
                "com.miui.securitycenter",
                "com.miui.permcenter.permissions.AppPermissionsEditorActivity"
            )
            intent.putExtra("extra_pkgname", "com.yourpackagename")
            startActivity(intent)
        }
    } catch (ignore: java.lang.Exception) {
    } catch (e: Exception) {
    }
}

fun getSystemProperty(propName: String): String? {
    val line: String
    var input: BufferedReader? = null
    try {
        val p = Runtime.getRuntime().exec("getprop $propName")
        input = BufferedReader(InputStreamReader(p.inputStream), 1024)
        line = input.readLine()
        input.close()
    } catch (ex: IOException) {
        return null
    } finally {
        if (input != null) {
            try {
                input.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
    return line
}
}

enter image description here

enter image description here

Some source of the code : https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/488 https://gist.github.com/Muyangmin/e8ec1002c930d8df3df46b306d03315d

Share:
811
Nimr Sawafta
Author by

Nimr Sawafta

Updated on January 01, 2023

Comments

  • Nimr Sawafta
    Nimr Sawafta over 1 year

    Im using oneSignal 3.2.3 on flutter 2.5, And I can receive the notification, and once I click it doesn't open my app, but the notification open handle is working and loads the data from the notification, but not opening the app on the screen, but using the same behavior on IOS it is working.

    and on build the app I'm getting this:

    The plugin `onesignal_flutter` uses a deprecated version of the Android embedding.
    To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. Otherwise, consider removing it
    since a future release of Flutter will remove these deprecated APIs.
    If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.
    

    flutter doctor:

    [✓] Flutter (Channel stable, 2.5.0, on macOS 11.4 20F71 darwin-x64, locale en-MA)
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    [✓] Xcode - develop for iOS and macOS
    [✓] Chrome - develop for the web
    [✓] Android Studio (version 4.2)
    [✓] VS Code (version 1.58.0)
    [✓] Connected device (2 available)
    
    • Jabbar
      Jabbar over 2 years
      could you check app settings menu from OneSignal Menu?
    • AzReddy
      AzReddy over 2 years
      Have you found any solution for this? It is happening only in the onesignal versions above 3.0.0
    • Nimr Sawafta
      Nimr Sawafta over 2 years
      I think you need to remove firebase fcm completely
  • Nimr Sawafta
    Nimr Sawafta about 2 years
    that's a good one, but not ideal to tell my users to do this just to allow notifications which is something that can be done from a button.