evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

Android - Not Getting Notification when app is killed #1014

Closed KwangsubAhn closed 5 years ago

KwangsubAhn commented 5 years ago

When openning an issue, please include following information for better support

  1. What version of RN and react-native-fcm are you running? "react-native": "^0.48.4", "react-native-fcm": "^9.6.2",

  2. What device are you using? (e.g iOS9 emulator, Android 6 device)? Android - MI Max

  3. Is your app running in foreground, background or not running? iOS - Running well. Android - only in kill state, doesnt get notification.

Here is my code. Please give me some advice. Thanks in advance. (I also tried to send a message through postman, same thing happened. iOS receives message well, but Android does not get the message only when the app is killed.)

AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mpowerapp" android:versionCode="23" android:versionName="1.0.0">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- Approximate location - If you want to use promptLocation for letting OneSignal know the user location. -->
<uses-permission android:name="android.permission.BLUETOOTH"/>         <!-- for Device Name -->

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

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="26" />

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

  <meta-data android:name="com.onesignal.BadgeCount" android:value="DISABLE" />

  <receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
  <receiver android:enabled="true" android:exported="true"  android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
    <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"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
  </receiver>

  <service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
  </service>
  <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
    <intent-filter>
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
  </service>

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
        <action android:name="fcm.ACTION.HELLO" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

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

Client Side - RN app.js (Listener.js is the same as example you provided)

registerKilledListener();
    export default class App extends React.Component {

    onReceived(notification) {
        console.log("Notification received: ", notification);
    }

    onOpened(openResult) {
        console.log('Message: ', openResult.notification.payload.body);
        console.log('Data: ', openResult.notification.payload.additionalData);
        console.log('isActive: ', openResult.notification.isAppInFocus);
        console.log('openResult: ', openResult);
    }

    onRegistered(notifData) {
        console.log("Device had been registered for push notifications!", notifData);
    }

    onIds(device) {
        console.log('Device info: ', device);
    }

    componentDidMount() {
        registerAppListener();

        FCM.getInitialNotification().then(notif => {
            this.setState({
                initNotif: notif
            })
        });
        FCM.setBadgeNumber(0);
        try {
            let result = FCM.requestPermissions({
                badge: false,
                sound: true,
                alert: true
            });
        } catch (e) {
            console.error(e);
        }

        FCM.getFCMToken().then(token => {
            //console.log("TOKEN (getFCMToken)", token);
            this.setState({
                token: token || ""
            })
        });

        if (Platform.OS === 'ios') {
            FCM.getAPNSToken().then(token => {
                console.log("APNS TOKEN (getFCMToken)", token);
            });
        }
    }

    render() {
        return ( <
            Provider store = {
                store
            } >
            <
            MenuContext >
            <
            Index / >
            <
            /MenuContext> <
            /Provider>
        );
    }
   }

Server Side - Java having remote notification code

Map<String, String> outDataPayload = new HashMap<String, String>();
   if (userInfo != null && userInfo.size() == 1 && userInfo.get(0).isNotifOn()) {
        outDataPayload.put("id", "id");
        outDataPayload.put("title", senderFullName);
        outDataPayload.put("body", message);
        outDataPayload.put("sound", "default");
        outDataPayload.put("priority", "high");
        outDataPayload.put("show_in_foreground", "true");
        outDataPayload.put("targetScreen", "detail");
        outDataPayload.put("badge", "2");
   }

Map<String, Object> customNotification = new HashMap<String, Object>();
customNotification.put("custom_notification", outDataPayload);
customNotification.put("inMessage", dataPayload);
customNotification.put("content_available", true);          
outMessage = new CcsOutMessage(senderFcmToken, messageId, customNotification);
//outMessage.setPriority(10);
Map<String, Object> map = MessageHelper.createAttributeMap(outMessage);

map.put("message_id", Util.getUniqueMessageId());
map.put("to", toRegId);
String jsonRequest = MessageHelper.createJsonMessage(map);
client.send(jsonRequest);
evollu commented 5 years ago

since it sounds like a xiaomi device, have you granted notification permission in phone settings?

KwangsubAhn commented 5 years ago

thanks evollu, you are right.

There was setting for notification permission in xiaomi device. Didn't know xiaomi phone has its own security setting.