React Native Android: Detect tap on a notification

3.1k Views Asked by At

I'm trying to detect a click on a notification.

I use react-native 0.61 and : react-native-notifications 3.1.4 (but get the same results with react-native-push-notification 3.1.9).

Everything works well but one thing: if I tap a notification or if I open my app from a notification with a notification from Firebase admin (or curl to FCM) I don't get those events fired:

Notifications.events().registerNotificationOpened

and

Notifications.getInitialNotification()

Howewer if I post a simple local notification with Notifications.postLocalNotification, everything works well.

Here is my standard curl request:

curl -X POST \
  https://fcm.googleapis.com/fcm/send \
  -H 'Authorization: key=mykey' \
  -H 'Content-Type: application/json' \
  -H 'Host: fcm.googleapis.com' \
  -d '{
 "to" : "cgh6P5BESPesyBJwYlaSPi:APA91bEuLwxgZq4JUlo2Su-_l1DqNVoAPzC2eHQy07IQlTuUcZNRuFiLzlbiFuCsHhU74SgYbrIkY7e2v4uQbYCspzRLlDrwrfha40Ozv613xwyYPtR3lJMaTuNcNAhGh8bdrmMY4B3N",
 "payload" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title", 
     "pushNotification": true,   
 }

Again, the notification appears but if I click on it, I don't get any event fired. However if I use postLocalNotification I get those events.

I have been struggling with this for days now, do I have to do something with intents in my AndroidManifest? Is there an example project with this working?

2

There are 2 best solutions below

3
On

After struggling for days, the problem was inside AndroidManifest where I had two activities, one for the splash screen and an other one for the notifications.

My AndroidManifest is now like this:

<activity
  android:name=".MainActivity"
  android:label="@string/app_name">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.INFO" />
  </intent-filter>
</activity>
<activity
  android:name=".SplashActivity"
  android:theme="@style/SplashTheme">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
0
On

1) Notifications.events().registerNotificationOpened, Notifications.events().registerNotificationReceivedBackground and Notifications.getInitialNotification() is not triggered if u are using with splash screen. 2) For custom icon and color on notification, add icon into all res/mipmap- and then use meta-data tag. Modify the AndroidManifest Like this for resolving above points:*

<uses-permission android:name="android.permission.INTERNET" />

<application
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:allowBackup="false"
  android:theme="@style/AppTheme">

  <meta-data  
  android:name="com.google.firebase.messaging.default_notification_icon" 
  android:resource="@mipmap/ic_notifications" />
  <meta-data
  android:name="com.google.firebase.messaging.default_notification_color"
  android:resource="@color/app_bg" />

  <!-- Add this SplashActivity -->
  <activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize" >
      <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.INFO" />
      </intent-filter>
    </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>