zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.73k stars 2.05k forks source link

Clicking on notification doesn't open Android app (remote push) #1806

Closed alfred2016decanet closed 3 years ago

alfred2016decanet commented 3 years ago

Hi,

i have a problem with push notifications, when the application is closed or running in the background, when i receive a push notification, clicking on it does not open the application. however when the application is open and has focus, the click seems to reload the application. Do you have any idea what might be causing this problem?

Sincerely

Dallas62 commented 3 years ago

Hi @alfred2016decanet

I suggest you to take a look at launchMode of your AndroidManifest: https://github.com/zo0r/react-native-push-notification/blob/master/example/android/app/src/main/AndroidManifest.xml#L21

Also check if there is no other Activity which could intercept the notification (before MainActivity / ReactActivity)

Regards,

alfred2016decanet commented 3 years ago

hi very fast reply tanks, but I do have the launchMode set up on manifest see below

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.decanet.moncrftoulouse">

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

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

<application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:allowBackup="true"
    android:launchMode="singleTask"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
    </receiver>

    <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
               android:value="true"/>
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
               android:resource="@android:color/white"/>

</application>

my application his a simple webview notting else :(.

Bres regard

Dallas62 commented 3 years ago

Unfortunately, without a reproducible exemple, I will not be able to help.

You can refer to the exemple project.

mikevercoelen commented 3 years ago

I have the same problem, notification shows up, but clicking on it make the notification disappear and not do anything, not open the app nothing.

@Dallas62 got any leads to what it might be? I've tried all bunch of variations of AndroidManifest.xml etc.

Dallas62 commented 3 years ago

Hi @mikevercoelen

Unfortunately, without a reproducible exemple, I will not be able to help.

You can refer to the exemple project.

mikevercoelen commented 3 years ago

I can't share a full working example unfortunately, it's company owned code.

It would be amazing if you can give me at least some leads.

One thing that I've been trying is ripping out my Splash screen library, could that affect it?

Here's my AndroidManifest.xml, do you spot any problems:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myapp">

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

  <!-- START React Native Push Notifications !-->

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

  <!-- END React Native Push Notifications !-->

  <uses-feature android:name="android.hardware.camera" android:required="false"/>
  <uses-feature android:name="android.hardware.camera.front" android:required="false"/>

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

    <activity android:name=".MainActivity" android:label="@string/app_name"
              android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
              android:launchMode="singleTask" android:windowSoftInputMode="adjustResize">

      <!-- Start Deeplinking !-->

      <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="rbsports"/>
      </intent-filter>

      <!-- End Deeplinking !-->
    </activity>

    <!-- Start SplashScreen !-->

    <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>

    <!-- End SplashScreen !-->

    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>

    <!-- Start React Native Push Notifications !-->

    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
                android:value="Live Message Notifications"/>
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                android:value="New messages"/>

    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
               android:resource="@drawable/notification_icon" />

    <meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
               android:resource="@color/app_primary"/>

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

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>

    <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
    <service
      android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
      android:exported="false" >
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
    </service>

    <!-- End React Native Push Notifications !-->

  </application>
</manifest>

The first thing I call in the App.js, is this function initializePushNotifications:

import PushNotification from 'react-native-push-notification'
import PushNotificationIOS from '@react-native-community/push-notification-ios'
import { FIREBASE_PROJECT_NUMBER } from '../config'
import { IS_ANDROID } from './device'

let deviceToken = null

export const initializePushNotifications = () => {
  if (!IS_ANDROID) {
    return null
  }

  return PushNotification.configure({
    onRegister: async (token) => {
      deviceToken = token
    },
    onNotification: notification => {
      notification.finish(PushNotificationIOS.FetchResult.NoData)
    },
    senderID: FIREBASE_PROJECT_NUMBER,
    requestPermissions: true
  })
}

export const getDeviceToken = () => {
  return deviceToken
}
Dallas62 commented 3 years ago

Hi @mikevercoelen

Can you share the file which is calling initializePushNotifications ?

I need to see the call-stack, I guess it's call in a component which is the reason of the issue.

Dallas62 commented 3 years ago

You can also try this solution: https://github.com/zo0r/react-native-push-notification/issues/1667

mikevercoelen commented 3 years ago

This is index.js (ultimate first file)

import 'react-native-get-random-values'
import 'react-native-gesture-handler'

import { AppRegistry } from 'react-native'
import App from './src/index'

AppRegistry.registerComponent('MyApp', () => App)

Then in App.js

import { initialize as initializeSentry } from './utils/sentry'

import React, { useState } from 'react'
import ErrorBoundary from './components/ErrorBoundary/ErrorBoundary'
import Error from './screens/Error/Error'
import AuthLoader from './components/AuthLoader/AuthLoader'
import SplashScreenLoader from './components/SplashScreenLoader/SplashScreenLoader'
import Main from './components/Main/Main'
import * as Sentry from '@sentry/react-native'
import SessionContext from './contexts/session'
import useProvideSession from './hooks/useProvideSession'
import { StatusBar } from 'react-native'
import { initializeGetStreamPushNotifications } from './utils/pushNotifications'

initializeGetStreamPushNotifications() // HERE IT GETS CALLED
initializeSentry()

const ignoredSentryTouchEventsNames = [
  'Provider'
]

const App = () => {
  const [error, setError] = useState(null)
  const session = useProvideSession()

  const handleError = error => {
    setError(error)
    Sentry.captureException(error)
  }

  if (error) {
    return <Error />
  }

  return (
    <Sentry.TouchEventBoundary ignoreNames={ignoredSentryTouchEventsNames}>
      <StatusBar
        backgroundColor='transparent'
        translucent
        barStyle='light-content'
      />
      <ErrorBoundary onError={handleError}>
        <SessionContext.Provider value={session}>
          <AuthLoader />
          <SplashScreenLoader />
          <Main />
        </SessionContext.Provider>
      </ErrorBoundary>
    </Sentry.TouchEventBoundary>
  )
}

export default App
mikevercoelen commented 3 years ago

You can also try this solution:

1667

Already tried, notifications are correctly received, but clicking on them doesn't make anything happen

Dallas62 commented 3 years ago

And did you tried the snippet bellow ? https://github.com/crazycodeboy/react-native-splash-screen/issues/289#issuecomment-433870271

package com.chatium.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        // Pass along FCM messages/notifications etc.
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            intent.putExtras(extras);
        }

        startActivity(intent);
        finish();
    }
}
mikevercoelen commented 3 years ago

Jup tried that, nothing, this is the behaviour I see:

https://www.loom.com/share/c7dd1a4b02ab4f00b6ce44301d6f681a

anna-kryva commented 3 years ago

Hi! I have faced this problem and tried all the possible solutions. As it turned out, there was a problem with a remote notification.

We use node-pushnotifications for this purpose. There is a field category in the notifications data. I haven't figured out the reason, but if this field is specified I receive a notification but can't open it. So, if you use this package, just skip this field.

I know that it is a special case but hope it will help someone

mikevercoelen commented 3 years ago

@anna-kryva thanks for sharing, we're using GetStream as push notifications this is the data they send out:


{
  "body": "{{ sender.name }} has posted a new message",
  "click_action": "OPEN_ACTIVITY_1",
  "sound": "default"
}
Dallas62 commented 3 years ago

@mikevercoelen So you are using GetStream as notification provider ? Note that this library is not made to work with all notification providers since they send different payload format.

I recommend you to look at this issue which is the same kind of issue with Batch provider. (https://github.com/zo0r/react-native-push-notification/issues/1801)

jcederholmdivisions commented 3 years ago

@mikevercoelen Try adding this intent-filter to your AndroidManifest nested inside of <activity android:name=".MainActivity"...

<intent-filter>
    <action android:name="OPEN_ACTIVITY_1" /> //This needs to be your click_action
    <category android:name="android.intent.category.DEFAULT" /> //Replace DEFAULT with your channel name
</intent-filter>
bananer commented 1 year ago

In case somebody still lands here: For me the problem only occurred on Android 13 and was fixed by updating firebase sdk and google play services to the latest versions.