0

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?

Simon
  • 6,025
  • 7
  • 46
  • 98

2 Answers2

0

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>
Simon
  • 6,025
  • 7
  • 46
  • 98
  • I tried adding the splash activity. I got the following error: "resource style/SplashTheme (aka com.myApp:style/SplashTheme) not found." Any suggestions? – Bilal Abdeen Jul 29 '20 at 21:32
  • You probably don't use a Splash screen thus you don't need to add this snippet of code – Simon Jul 29 '20 at 22:03
  • hey @Simon, I am too struggling with the issue you had for days now. I wonder how this code in the manifest file made it work for you. Asking because the default code I found in the file is exactly the same as the one you shared! :D (small difference though is instead of INFO, I have LAUNCHER but since I am not using splash screens I think that's correct) So, what exactly did you change and how that helped you? – tonkatata Aug 01 '20 at 15:11
0

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>

Ajmal Hasan
  • 885
  • 11
  • 9