Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to receive notification when app is closed #587

Closed
wahyu-handayani opened this issue Aug 11, 2022 · 7 comments
Closed

Unable to receive notification when app is closed #587

wahyu-handayani opened this issue Aug 11, 2022 · 7 comments

Comments

@wahyu-handayani
Copy link

wahyu-handayani commented Aug 11, 2022

Hi I am using onesignal_flutter: ^3.4.1 and successfully getting notification when I open my app. But when I close my app from background, I didn't get any notification although the status is delivered from onesignal
Here is android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and here is android/app/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

buildscript {
    repositories {
        // ...
        maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal
    }
    dependencies {
        // ...
        // OneSignal-Gradle-Plugin
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
    }
}


android {
    compileSdkVersion 32
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.onesignal_notif"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        minSdkVersion 21
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
}

is there something that I missed inside build.gradle ? I also open my question on https://stackoverflow.com/questions/73316209/unable-to-receive-notification-from-onesignal-when-app-is-closed-from-background

@krishnapandya
Copy link

Hello,

add OneSignal.OSRemoteNotificationReceivedHandler code in Native and also meta tag for it..

@williamwiseki
Copy link

Hello,

add OneSignal.OSRemoteNotificationReceivedHandler code in Native and also meta tag for it..

can you show how to implement this??

@krishnapandya
Copy link

krishnapandya commented Sep 29, 2022

add NotificationServiceExtension.kt at native side

          import android.content.Context
          import com.onesignal.OSNotificationReceivedEvent
          import com.onesignal.OneSignal
          import android.app.Notification              
          import android.R
          import androidx.core.app.NotificationCompat              
          
          class NotificationServiceExtension : OneSignal.OSRemoteNotificationReceivedHandler {
              override fun remoteNotificationReceived( context: Context,notificationReceivedEvent: OSNotificationReceivedEvent
              ) {
                  val notification = notificationReceivedEvent.notification
                  val mutableNotification = notification.mutableCopy()
                  notificationReceivedEvent.complete(mutableNotification)
              }
          }

add that in manifest::

  <meta-data
                android:name="com.app.onesignal.NotificationServiceExtension"
                android:value="com.app.view.services.NotificationServiceExtension" />

@davetebonfire0302
Copy link

add NotificationServiceExtension.kt at native side

          import android.content.Context
          import com.onesignal.OSNotificationReceivedEvent
          import com.onesignal.OneSignal
          import android.app.Notification              
          import android.R
          import androidx.core.app.NotificationCompat              
          
          class NotificationServiceExtension : OneSignal.OSRemoteNotificationReceivedHandler {
              override fun remoteNotificationReceived( context: Context,notificationReceivedEvent: OSNotificationReceivedEvent
              ) {
                  val notification = notificationReceivedEvent.notification
                  val mutableNotification = notification.mutableCopy()
                  notificationReceivedEvent.complete(mutableNotification)
              }
          }

add that in manifest::

  <meta-data
                android:name="com.app.onesignal.NotificationServiceExtension"
                android:value="com.app.view.services.NotificationServiceExtension" />

how about for java? Thanks

@rituapplocum
Copy link

@krishnapandya Is the above solution working for you?

@tukuttemita
Copy link

This is a fatal problem, whether to use this pub or not.

@jennantilla
Copy link
Contributor

Hello everyone! Apologies that this issue has gone stale.

Based on the original issue description, it sounds as though notifications are not received when the app is in a force-closed state. While undesirable, this is expected behavior on some Android devices. We have some troubleshooting steps to try here.

I'm going to close this issue but please @ mention me if there are still questions or concerns.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants