React-native-onesignal: onOpened/onReceived events do not firing on android

Created on 9 Jul 2018  ·  35Comments  ·  Source: OneSignal/react-native-onesignal

Description:
Hi everybody. I'm having troubles with onOpened/onReceived events on android. Behavior is different on emulator and on a real device, but exact push message is delivered in both cases.

On emulator:

  1. Foreground - onReceived event is invoked, onOpened is invoked only when push is tapped (correct)
  2. Background active - none fired at all (incorrect)
  3. Background inactive (app is closed) - onOpened is invoked only when push is tapped (correct)

On a real device:

  1. Foreground - none fired at all (incorrect)
  2. Background active - none fired at all (incorrect)
  3. Background inactive (app is closed) - onOpened is invoked only when push is tapped (correct)

Environment

  1. react-native-onesignal: "3.2.4"
  2. react-native: "0.50.4"
  3. compileSdkVersion: "27"
  4. buildToolsVersion: "27.0.3"
  5. gradle: "4.1"

Steps to Reproduce Issue:
Can't provide steps to reproduce due to my company's policy of confidence.

Anything else:
android/build.gradle

```// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}

allprojects {
repositories {
maven { url "https://jitpack.io" } // used for MPAndroidChart
mavenLocal()
google()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
}

`android/app/build.gradle`

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: "com.android.application"

import com.android.build.OutputFile

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

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

/**

  • The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
  • and bundleReleaseJsAndAssets).
  • These basically call react-native bundle with the correct arguments during the Android build
  • cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
  • bundle directly from the development server. Below you can see all the possible configurations
  • and their defaults. If you decide to add a configuration block, make sure to add it before the
  • apply from: "../../node_modules/react-native/react.gradle" line.
    *
  • project.ext.react = [
  • // the name of the generated asset file containing your JS bundle
  • bundleAssetName: "index.android.bundle",
    *
  • // the entry file for bundle generation
  • entryFile: "index.android.js",
    *
  • // whether to bundle JS and assets in debug mode
  • bundleInDebug: false,
    *
  • // whether to bundle JS and assets in release mode
  • bundleInRelease: true,
    *
  • // whether to bundle JS and assets in another build variant (if configured).
  • // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
  • // The configuration property can be in the following formats
  • // 'bundleIn${productFlavor}${buildType}'
  • // 'bundleIn${buildType}'
  • // bundleInFreeDebug: true,
  • // bundleInPaidRelease: true,
  • // bundleInBeta: true,
    *
  • // whether to disable dev mode in custom build variants (by default only disabled in release)
  • // for example: to disable dev mode in the staging build type (if configured)
  • devDisabledInStaging: true,
  • // The configuration property can be in the following formats
  • // 'devDisabledIn${productFlavor}${buildType}'
  • // 'devDisabledIn${buildType}'
    *
  • // the root of your project, i.e. where "package.json" lives
  • root: "../../",
    *
  • // where to put the JS bundle asset in debug mode
  • jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
    *
  • // where to put the JS bundle asset in release mode
  • jsBundleDirRelease: "$buildDir/intermediates/assets/release",
    *
  • // where to put drawable resources / React Native assets, e.g. the ones you use via
  • // require('./image.png')), in debug mode
  • resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
    *
  • // where to put drawable resources / React Native assets, e.g. the ones you use via
  • // require('./image.png')), in release mode
  • resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
    *
  • // by default the gradle tasks are skipped if none of the JS files or assets change; this means
  • // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
  • // date; if you have any other folders that you want to ignore for performance reasons (gradle
  • // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
  • // for example, you might want to remove it from here.
  • inputExcludes: ["android/", "ios/"],
    *
  • // override which node gets called and with what additional arguments
  • nodeExecutableAndArgs: ["node"],
    *
  • // supply additional arguments to the packager
  • extraPackagerArgs: []
  • ]
    */

project.ext.react = [
cliPath: "node_modules/haul/bin/cli.js"
]

apply from: "../../node_modules/react-native/react.gradle"

/**

  • Set this to true to create two separate APKs instead of one:

    • An APK that only works on ARM devices


    • An APK that only works on x86 devices

  • The advantage is the size of the APK is reduced by about 4MB.
  • Upload all the APKs to the Play Store and people will download
  • the correct one based on the CPU architecture of their device.
    */
    def enableSeparateBuildPerCPUArchitecture = false

/**

  • Run Proguard to shrink the Java bytecode in release builds.
    */
    def enableProguardInReleaseBuilds = false

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"

defaultConfig {
    applicationId "com.binomysteryshopper"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}
signingConfigs {
      release {
          if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
              storeFile file(MYAPP_RELEASE_STORE_FILE)
              storePassword MYAPP_RELEASE_STORE_PASSWORD
              keyAlias MYAPP_RELEASE_KEY_ALIAS
              keyPassword MYAPP_RELEASE_KEY_PASSWORD
          }
      }
  }
splits {
    abi {
        reset()
        enable enableSeparateBuildPerCPUArchitecture
        universalApk false  // If true, also generate a universal APK
        include "armeabi-v7a", "x86"
    }
}
buildTypes {
    release {
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        signingConfig signingConfigs.release
    }
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // For each separate APK per architecture, set a unique version code as described here:
        // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
        def versionCodes = ["armeabi-v7a":1, "x86":2]
        def abi = output.getFilter(OutputFile.ABI)
        if (abi != null) {  // null for the universal-debug, universal-release variants
            output.versionCodeOverride =
                    versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
        }
    }
}

}

dependencies {
compile (project(':react-native-onesignal')){
exclude group: "com.google.android.gms"
}
compile ("com.google.android.gms:play-services-base:12.0.1") {
force = true;
}
compile project(':react-native-sound')
compile project(':react-native-audio')
compile project(':react-native-fetch-blob')
compile project(':react-native-fs')
compile project(':react-native-geocoder')
compile project(':react-native-image-crop-picker')
compile project(':react-native-version-check')
compile project(':react-native-charts-wrapper')
compile project(':react-native-spinkit')
compile project(':react-native-svg')
compile project(':react-native-maps')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}

`android/app/src/main/java/com/binomysteryshopper/MainApplication.java`

package com.binomysteryshopper;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.geektime.rnonesignalandroid.ReactNativeOneSignalPackage;
import com.zmxv.RNSound.RNSoundPackage;
import com.rnim.rn.audio.ReactNativeAudioPackage;
import com.RNFetchBlob.RNFetchBlobPackage;
import com.rnfs.RNFSPackage;
import com.reactnative.ivpusic.imagepicker.PickerPackage;
import io.xogus.reactnative.versioncheck.RNVersionCheckPackage;
import com.github.wuxudong.rncharts.MPAndroidChartPackage;
import com.react.rnspinkit.RNSpinkitPackage;
import com.horcrux.svg.SvgPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.devfd.RNGeocoder.RNGeocoderPackage;
import com.facebook.soloader.SoLoader;
import com.airbnb.android.react.maps.MapsPackage;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
        new ReactNativeOneSignalPackage(),
        new RNSoundPackage(),
        new ReactNativeAudioPackage(),
        new RNFetchBlobPackage(),
        new RNFSPackage(),
        new PickerPackage(),
        new RNVersionCheckPackage(),
        new MPAndroidChartPackage(),
        new RNSpinkitPackage(),
        new RNGeocoderPackage(),
        new SvgPackage(),
        new MapsPackage()
  );
}

@Override
protected String getJSMainModuleName() {
  return "index";
}

};

@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}

@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}

`AndroidManifest.xml`

package="com.binomysteryshopper">









android:minSdkVersion="16"
android:targetSdkVersion="22" />

android:name=".MainApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:largeHeap="true"
android:theme="@style/AppTheme">

<activity
  android:name=".MainActivity"
  android:launchMode="singleTop"
  android:label="@string/app_name"
  android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
  android:screenOrientation="portrait"
  android:windowSoftInputMode="adjustResize"
  android:theme="@style/AppTheme">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="binoshopper" />
    </intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />


`my OneSignal init`

componentWillMount() {
OneSignal.init(ONESIGNAL_APP_ID, {
kOSSettingsKeyAutoPrompt: true,
kOSSettingsKeyInAppLaunchURL: false,
kOSSSettingsKeyPromptBeforeOpeningPushURL: true,
})
OneSignal.inFocusDisplaying(2)

OneSignal.addEventListener('received', this.onPushNotificationReceived)
OneSignal.addEventListener('opened', this.onPushNotificationOpened)

}

componentWillUnmount() {
OneSignal.removeEventListener('received', this.onPushNotificationReceived)
OneSignal.removeEventListener('opened', this.onPushNotificationOpened)
}

onPushNotificationReceived(notification) {
Alert.alert('onReceived') // for tests on real device
}

onPushNotificationOpened(openResult) {
Alert.alert('onOpened') // for tests on real device
}
```

Most helpful comment

@sofianegargouri Here's the PR: https://github.com/geektimecoil/react-native-onesignal/pull/592

We'll be releasing an update either tomorrow or the day after.

All 35 comments

On IOS it's working perfect. Unfortunately i can't provide logs from android logcat, because i don't have a real device. I was testing on my far away friend's device due TeamViewer.

I also tried to OneSignal.setLogLevel(0, 6) on a real device. But none errors were found, all requests succeeded, only events didn't fired.

Hi @Ble3k , we have been quite busy working on a new Flutter SDK recently and haven't had time to investigate your issue yet, however I will investigate tomorrow and will get back to you shortly, apologies for the delay!

Hi,

Just wanted to say I see the same behavior on 3.2.5 on a dev device. Weird because I haven't changed anything recently and I'm pretty sure it worked some days ago, as my focused pushs are displayed as in-app toasts I remember having tested these and it was working.

I won't investigate further because we decided to move to normal push notifications even when app is focused

@Nightsd01 Thx, will wait for your investigation.

On Android facing delay in receiving notifications, tested on my android phone and received notification after 30-40 mins, multiple times tested

Any update on this? I am having the same issue on Android device (Samsung Galaxy S7).

None of the onOpened or onReceived events are being fired, however, the notification is being received. We need to be able to navigate the user to a specific screen when the onOpened event is fired.

Does anyone have problem with events (received, opened) not being fired after reload react native android app by double pressing R, or using react-native-restart package.

@ackosumane I actually use Expo and Updates.reload() very often in my dev workflow.

And you are right:

  • I am able to get the callbacks fired on first app start
  • I don't see the callbacks fired after the first app reload

@Nightsd01 So it looks like OneSignal RN Android integration currently does not support app reloads very well. It is not as impactful as I thought as most of the time the user will not reload the app, but there are production cases in which we want the user to reload the app (OverTheAir updates like Expo/CodePush, or user logout?). In such cases it would be great that after a reload the pushes are not broken ;)

@Nightsd01 Hi, any updates ?

Hello there, I also face the same problem.Any solutions for this ???

As an update on this issue: we plan to fix this in the next update!

@Nightsd01 Any date ? Is there a branch we can fork with the fix ?

@sofianegargouri Here's the PR: https://github.com/geektimecoil/react-native-onesignal/pull/592

We'll be releasing an update either tomorrow or the day after.

thanks @Nightsd01, this issue can be closed I guess

can you publish a new npm release with the fix please?

For anyone who stuck with this issue, the code from master is working well now.

Just run the following:

npm install git+https://github.com/geektimecoil/react-native-onesignal#master --save

@eabuslaiman30 Still facing an issue. I am using git+https://github.com/geektimecoil/react-native-onesignal#master as you mentioned above. Still none of events is triggered in android. Anyone can suggest which latest version is stable? Previously I was using 3.0.5 version which was working fine except app is killed and notification is not received in android 8.0 (Oreo). To fix that issue I update to latest version and i'm getting this error .

@eabuslaiman30 @Nightsd01 I also tried and like @shahchaitanya I still face the issue.

I have made a PR that seems to fix the listeners not subscribing again: https://github.com/geektimecoil/react-native-onesignal/pull/593

@shahchaitanya you can edit the local project Java code with this tiny change and this should solve your problem temporarily.

Hey @slorber Your solution trigger onOpened and onReceived method. But onIds event is not triggered yet? Did you check onIds event is triggered or not in your code? I was thinking stick to react-native-onesignal version 3.0.5 till latest version will become stable.

I just tested and all 3 callbacks fire on app restart for me, starting with onIds.

Even if onIds didn't fire it wouldn't be a big deal for me as the ids are unlikely to change across restarts and they have already been sent to my backend.

@slorber For me it's big deal if onIds didn't fire when restarting an app. I store idToken in local storage and backend also. On signout i removed it from backend And Somehow if user clear local storage, then how can i get idToken of a user if onIds didn't fire every time. In previous version until 3.2.0 (Major Update) , onIds is triggered every time while restarting an app.

Hey guys @slorber @shahchaitanya. Let me copy some of my implementation code, maybe it will help you (I needed to add some lines of code that weren't required with a previous version).

OneSignal.init('YOUR_APP_ID');
OneSignal.addEventListener('ids', this.onIds);
OneSignal.configure();

For example, I was not using the "init" function in previous versions since the initialization apparently was being done from the Java code.

Also, i'm using npm install git+https://github.com/geektimecoil/react-native-onesignal\#master --save as a dependency and i'm receiving three events correctly (onIds, received, opened).

I'm also using init and configure and remember there was an issue with ids not firing when not calling configure

    OneSignal.init(OneSignalAppId, OneSignalSettings);
    OneSignal.addEventListener('received', onReceived);
    OneSignal.addEventListener('opened', onOpened);
    OneSignal.addEventListener('ids', onIds);
    // Quickfix for ids event not firing!
    // See https://github.com/geektimecoil/react-native-onesignal/issues/200#issuecomment-288579373
    OneSignal.configure();
    OneSignal.inFocusDisplaying(inFocusDisplaying);
    OneSignal.setSubscription(true);
    OneSignal.syncHashedEmail(email);
    OneSignal.sendTags(tags);

@slorber @eabuslaiman30 Now it's working Fine. I missed to add OneSignal.configure(). Previously i didn't need it to trigger onIds listener. Hey @slorber now i don't need to add that patch that you mentioned above. @eabuslaiman30 solution is working absolutely fine.

Perfect @shahchaitanya, I'm glad it worked!

We will be coming out with an official release soon including this fix.

I recently fixed a somewhat significant issue with the Android SDK, so I am waiting for that PR to get reviewed before I release an official react-native update.

Thanks for your patience everyone! In the mean time you can simply use master

@shahchaitanya the ids event is intended to simply fire whenever the user's ID's or push token with OneSignal change. If they didn't change since the last time they used your app, the events won't fire by default.

This is confusing legacy code, I intend to refactor it in the next major release to be a lot more intuitive.

You can force them to fire by calling configure(), but I wouldn't recommend it. A more elegant solution is to call OneSignal.getPermissionSubscriptionState() which will contain the user's OneSignal ID

Issue should now be fixed in 3.2.6 (along with several other things). Thanks for your patience everyone!

Hi! I'm still getting this issue on Android (not fully checked iOS).

  • App in focus - Opened event fires correctly
  • App dismissed - Opened event fires correctly
  • App in background - Opened event never fires

I am on the 3.2.6 release and my event listener set up is...

componentDidMount() {

        OneSignal.init(APP_ID, { kOSSettingsKeyAutoPrompt: true });

        OneSignal.addEventListener('ids', this.onIds);

        OneSignal.addEventListener('opened', (result) => alert('text'));

        OneSignal.inFocusDisplaying(2);

        // OneSignal.setLogLevel(0, 6);

        OneSignal.configure();
}

componentWillUnmount() {
    OneSignal.removeEventListener('ids', this.onIds);
    OneSignal.removeEventListener('opened', (result) => alert('text'));
}

@joefazz I am not able to reproduce your issue, can you try running yarn remove react-native onesignal and yarn add react-native-onesignal just to make sure it's updated to 3.2.6? If so I will reopen the issue.

Does the issue happen 100% of the time for you?

Are you able to reproduce this issue in our demo project?

It does happen 100% of the time and still occured after removing and readding.

I will try to reproduce sometime later today and I'll edit this comment with my findings.

I don't know how relevant it is but in the project I'm working on we were on version 3.0.9 and upgraded all the way to 3.2.6. I ran react-native unlink and then relinked, upgraded iOS manually and removed all the native initialisation to use the OneSignal.init() method.

EDIT: Not sure what happened but it's working 100% of the time on Android now thanks for your help sorry to comment on a closed issue 😅

If anyone sees this issue again in 3.2.6 or higher, please feel free to open a new issue.

@joefazz Glad to hear it! I fixed the issue that caused the initial problem but it's entirely possible there's some sort of race condition going on. If you see this problem again please feel free to open a new issue

In my app the new version fix my issues and I don't see the behavior reported by @joefazz

Hi all, I have the same issue OneSignal.addEventListener('opened', (result) => console.log(result)); is not fired from the second launch of app. In a background mode first notification is getting 'opened' event, kill the app => get notification => 'opened' will never fire again.

only android - 8 verison
only after second notification when app was in a background
react-native-onesignal - 3.9.1

I'm having same problem. opened wont call when app is background is there any fix

@kostiantyn-solianyk @shrolr ,
So to summarize, the 'opened' event doesn't fire the second time the app is opened in Android 8.

The tradition events are being deprecated in the coming major release (now in Beta). I recommend migrating to the beta and attempting to reproduce the issue.

If the issue persists, please do let us know by opening a new issue.

Cheers

Was this page helpful?
0 / 5 - 0 ratings