Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Not getting push notifications when app is closed/killed/background #26856

Closed
ramisettysrini opened this issue Oct 14, 2019 · 5 comments
Labels
Bug Platform: Android Android applications. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@ramisettysrini
Copy link

App receiving push notifications when the app is in foreground and using it , but when app is closed/ killed/in background not receiving the push notifications .

React Native version:
react-native-cli: 2.0.1
react-native: 0.60.5

Steps To Reproduce

1.Here is AndroidManifest.xml:

<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="false"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustPan">
    <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" />
  <!-- push notification -->
  <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
            android:value="AWS SNS"/>
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                android:value="AWS SNS"/>
    <!-- Change the resource name to your App's accent color - or any other color you want -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                android:resource="@android:color/white"/>

    <!-- < Only if you're using GCM or localNotificationSchedule() > -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>
    <!-- < Only if you're using GCM or localNotificationSchedule() > -->

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

    <!-- < Only if you're using GCM or localNotificationSchedule() > -->
    <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerServiceGcm"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- </ Only if you're using GCM or localNotificationSchedule() > -->

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

2. android/app/build.gradle

implementation project(':react-native-push-notification')
3.package.json

"dependencies": {

"react-native-push-notification": "^3.1.9",
}

4. Sample Coode PushNotificationHandler.js

import PushNotification from 'react-native-push-notification';
class PushNotificationHandler extends Component {

constructor(props) {
super(props)

}

componentDidMount() {
console.log('component did mount in pushN')
if (Platform.OS === "android") {
PushNotification.configure({

    // (optional) Called when Token is generated (iOS and Android)
    onRegister: function (token) {
      console.log('TOKEN:', token);
  

    },

    // (required) Called when a remote or local notification is opened or received
    onNotification: function (notification) {
      console.log('NOTIFICATION:', notification);
      console.log('NOTIFICATION:', notification.default);
     

    },

    // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
    senderID: "my gcm send Id",

    // IOS ONLY (optional): default: all - Permissions to register.
    permissions: {
      alert: true,
      badge: true,
      sound: true
    },

    // Should the initial notification be popped automatically
    // default: true
    popInitialNotification: true,

    /**
      * (optional) default: true
      * - Specified if permissions (ios) and token (android and ios) will requested or not,
      * - if not, you must call PushNotificationsHandler.requestPermissions() later
      */
    requestPermissions: true,
  });
}

render() {
return null
}
}

export default PushNotificationHandler
@react-native-bot react-native-bot added the Platform: Android Android applications. label Oct 14, 2019
@malikid
Copy link

malikid commented Dec 24, 2019

I ran into a similar issue, which is the notification doesn't work when the app is not in the background, and it was related to the battery optimization feature on some devices. Found the reason for my issue and not sure if they are the same. Hope it helps 🙂
https://stackoverflow.com/questions/55719666/android-push-notifications-not-working-when-app-is-closed

@stale
Copy link

stale bot commented Mar 23, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 23, 2020
@jan-miksik
Copy link

jan-miksik commented Mar 23, 2020

Hi, to receive notification in background helps post message only with "data" and no notification part like this:

{
 "to" : "cRXoVRCWRdc:APA91bFFS_JGN07lvw2U_OP-o0Pr-JkwcY4adY0ZUPkGG-Ixl4Xe2C6wvZVIvmOWE...",
 "android":{
      "priority":"high"
    },
 "data" : {
     "body" : "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
     "title": "Notification Title"
 },
 "priority":"high"
}

I used https://wix.github.io/react-native-notifications/docs/getting-started
And postman to send notification
Still don't no how to manage push notification if app is closed.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 23, 2020
@stale
Copy link

stale bot commented Jun 21, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jun 21, 2020
@stale
Copy link

stale bot commented Jun 28, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Jun 28, 2020
@facebook facebook locked as resolved and limited conversation to collaborators Jun 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Platform: Android Android applications. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

4 participants