notifee: App does not open on notification click (should "pressAction" be a default without needing to specify it?)

Apparent bug

It seems like the app does not open when clicking the notification body.

Environment

react-native: 0.66.4 @notifee/react-native: 4.0.1, device/api: Pixel_5_API_31

Reproduction steps

  1. npx react-native init repro
  2. npm i @notifee/react-native
  3. add android:exported="true" to AnroidManifest.xml
  4. update build.gradle with compileSdkVersion = 31 targetSdkVersion = 31
  5. add provided code of minimal example
  6. build app
  7. click button to display notification
  8. minimize app and click notification
  9. in my case, the notification disappears after a short moment but the app does not open

minimal example:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React from 'react';
import {View, Button} from 'react-native';

import notifee from '@notifee/react-native';

const App = () => {
  async function onDisplayNotification() {
    const channelId = await notifee.createChannel({
      id: 'default',
      name: 'Default Channel',
    });
    await notifee.displayNotification({
      title: 'title',
      body: 'body',
      android: {
        channelId,
        pressAction: {
          id: 'default',
        },
      },
    });
  }

  return (
    <View>
      <Button
        title="Display Notification"
        onPress={() => onDisplayNotification()}
      />
    </View>
  );
};

export default App;

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 44 (14 by maintainers)

Most upvoted comments

This works for me with flavors / applicationIdSuffice:


          await notifee.displayNotification({
            title: messageOptions.title,
            body: messageOptions.body,
            android: {
              channelId: BackgroundTaskService.CONNECTION_REQUEST_NOTIFICATION_CHANNEL_ID,
              smallIcon: 'drawable/minilogo_bw',
              pressAction: {
                id: 'default',
                launchActivity: 'default',
                launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
              },
            },
          });

Just updating here on status, the latest release v4.1.0 contains a fix for apps targeting API 31 and devices running Android 12.

 android: {
                    pressAction: {
                      id: 'default',
                      launchActivity: 'com.package.MainActivity', // <-- here you can add the package name of your app
                    },
                    channelId,
                  }

The solution for me, thanks team

@alexanderdavide I added an action to actions array in android property when creating trigger notification, here’s sample code and here’s more info:

await notifee.createTriggerNotification({
  id: '123',
  title: 'title',
  body: 'body',
  android: {
    actions: [
      {
        title: 'test',
        pressAction: {
          id: 'default',
          launchActivity: 'com.packagename.MainActivity', // <-- here you can add the package name of your app
        },
      },
    ],
    channelId: '123',
  },
}, trigger)

I found out that I might be encountering a different issue, notifications don’t open app even on older versions (as I mentioned in previous comment, I have API 30 which is Android 11). Today I tested all older versions down to Android 9, none of them worked.

But when I tried example project from the repo, it worked. So I’m assuming there’s something wrong with my setup. I’ll let you know if I manage to fix it or figure out what the issue is.

this is definitely possible, it will be a breaking change for developers who haven’t set a pressAction intentionally. My main hesitation to do this is for the scenario you don’t want a pressAction, might be harder to specify that. But in terms of implementation, this would be very simple to do. It’s just on the js level to add pressAction: { id: 'default' } as a default value if not set.

@alexanderdavide I didn’t test it on API 31, so I’m not sure, but I think the workaround from #250 is still necessary. I was already on that SDK version mentioned in the workaround.

@effektsvk @mikehardy @helenaford To clarify: Should this work on API 31 too? I’ve just tried this

pressAction: {
  id: 'default',
  launchActivity: 'default',
  launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
}

on the setup I’ve opened the issue with but opening the app upon notification click remains dysfunctioning, hence I’m still working on 30 as #250 proposes which works with just pressAction.id defined.

@effektsvk ah yeah, thanks. That’s good to know. I think that is the issue 🤔 It probably makes sense to default to id: default if pressAction isn’t defined.

Okay, I might be able to take a look at it today, if I make some sense of it, I’ll let you know. (I’m a bit scared because I don’t have a lot of experience with android environment and packages, but I guess it’s still just code, nothing to worry about, right? 😅😅)

Again, thank you very much for your help! ❤️

@alexanderdavide In android/app/build.gradle we have product flavors defined like this, and then build the app with react-native run-android --appIdSuffix development --variant deploymentDevelopmentDebug:

android {
    // ...
    flavorDimensions "deployment"
    productFlavors {
        deploymentLocal {
            dimension "deployment"
            applicationIdSuffix ".local"
	    }

        deploymentDevelopment {
            dimension "deployment"
            applicationIdSuffix ".development"
        }

        deploymentTesting {
            dimension "deployment"
            applicationIdSuffix ".testing"
        }

        deploymentProduction {
            dimension "deployment"
        }
    }
    // ...
}

@effektsvk Sorry, I thought your device is on API 30 but you may still have targetSdkVersion on 31. So, you had the issues while being on targetSdkVersion 30? Then there must be another problem. This is my buildscript.ext with the working workaround:

ext {
      buildToolsVersion = "30.0.2"
      minSdkVersion = 23
      compileSdkVersion = 31
      targetSdkVersion = 30
      ndkVersion = "21.4.7075529"
}

@mikehardy Thanks. I always try to give the best reports I can to make your lives a little easier.

@effektsvk Actually, in my real application where I experience this problem I’m using trigger notifications too. For this report, I opted for a simpler example.