Pushwoosh / pushwoosh-react-native-plugin

Other
57 stars 43 forks source link

No task registered for key RNFirebaseBackgroundMessage (Android) #94

Closed ymcewen closed 4 years ago

ymcewen commented 4 years ago

Hi There,

I've followed this guide (https://docs.pushwoosh.com/platform-docs/pushwoosh-sdk/cross-platform-frameworks/react-native/integrating-react-native-plugin) on the website but it just isn't working in my project.

When I use the same init credentials in the example on GitHub, I get the notification.

My error is No task registered for key RNFirebaseBackgroundMessage

I followed the example to the letter so not really sure whats going on. Ive seen some things online about the error but none seem to help.

Here is my index.js file:


import App from './src/routes/routes';
import { name as appName } from './app.json';
import { setJSExceptionHandler, setNativeExceptionHandler } from 'react-native-exception-handler';
import firebase from 'react-native-firebase';

const reporter = (error) => {
    // Logic for reporting to devs
    // Example : Log issues to github issues using github apis.
    console.log(error); // sample
    firebase.crashlytics().log(error);
};

const errorHandler = (e, isFatal) => {
    if (isFatal) {
        reporter(e);
        Alert.alert(
            'Unexpected error occurred',
            `Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
            We have reported this to our team ! Please close the app and start again!
            `,
            [{
                text: 'Close',
                onPress: () => {
                    BackHandler.exitApp();
                }
            }]
        );
    } else {
        firebase.crashlytics().log(e);
        console.log(e); // So that we can see it in the ADB logs in case of Android if needed
    }
};

setJSExceptionHandler(errorHandler);
setNativeExceptionHandler(exceptionString => { });

import Pushwoosh from 'pushwoosh-react-native-plugin';

AppRegistry.registerComponent(appName, () => App);

DeviceEventEmitter.addListener('pushOpened', (e) => {
    console.warn("pushOpened: " + JSON.stringify(e));
    alert(JSON.stringify(e));
  });

DeviceEventEmitter.addListener('pushReceived', (e) => {
console.warn("pushReceived: " + JSON.stringify(e));
// shows a push is received. Implement passive reaction to a push, such as UI update or data download.
});

  Pushwoosh.init({ "pw_appid" : "MY ID", "project_number":"MY NUMBER"});

  Pushwoosh.register(
    (token) => {
      console.warn("Registered for pushes: " + token);
      Pushwoosh.getPushToken(function(token) {
          console.warn("Push token: " + token);

          // Geolocation tracking example
          //PushwooshGeozones.startLocationTracking();
          //PushwooshGeozones.stopLocationTracking();
      });
    },
    (error) => {
      console.warn("Failed to register: " + error);
    }
  );

  Pushwoosh.getHwid((hwid) => {
    console.warn("Pushwoosh hwid: " + hwid);
  });```
wfhm commented 4 years ago

Hi @ymcewen, please let me pull some time to check it.

wfhm commented 4 years ago

@ymcewen,

Could you please share the complete crash log, package.json and AndroidManifest.xml?

ymcewen commented 4 years ago

Hi @wfhm

I've now reverted my changes but to be clear- there was no crash. It was just a warning in the console when the push notification should have been received.

My AndroidManifest.xml was untouched as the only reference in the docs was to add a line to disable foreground push right?

AndroidManifest.xml

  package="com.myapp.myapp">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  <uses-permission android:name="android.permission.READ_CONTACTS" />
  <uses-permission android:name="android.permission.READ_PROFILE" />
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="android.permission.CAMERA"/>

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
        android:usesCleartextTraffic="true"
      android:theme="@style/AppTheme">
      <!-- <meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" /> -->
  <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
       notification message. See README(https://goo.gl/6BKBk7) for more. -->
  <!-- <meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" /> -->
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize" android:screenOrientation="portrait" android:launchMode="singleTop">
        <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" />
       <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>
  <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />

     <meta-data
     android:name="com.google.android.geo.API_KEY"
     android:value="AIzaSyBEWDCuN9EP-ZJp6fWr3EVtzAI_SBMC8cM"/>
        <uses-library android:name="org.apache.http.legacy" android:required="false"/>

        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>

    </application>

</manifest>

package.json (now without the pushwoosh package)

  "name": "myapp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/async-storage": "^1.6.1",
    "@react-native-community/viewpager": "^3.1.0",
    "axios": "^0.19.0",
    "eventemitter3": "^4.0.0",
    "libphonenumber-js": "^1.7.22",
    "lodash": "^4.17.15",
    "lottie-ios": "^3.0.3",
    "lottie-react-native": "^3.1.1",
    "moment": "^2.24.0",
    "moment-array-dates": "^2.0.3",
    "moment-range": "^4.0.2",
    "moment-timezone": "^0.5.27",
    "moment-weekday-calc": "^1.1.4",
    "react": "16.8.6",
    "react-content-loader": "^4.3.2",
    "react-native": "0.60.4",
    "react-native-advertising-id": "^1.0.10",
    "react-native-app-intro-slider": "^3.0.0",
    "react-native-atoz-list": "^1.0.3",
    "react-native-calendars": "^1.213.0",
    "react-native-check-box": "^2.1.7",
    "react-native-contacts": "^4.0.3",
    "react-native-country-picker-modal": "^0.8.0",
    "react-native-device-info": "^2.3.2",
    "react-native-exception-handler": "^2.10.8",
    "react-native-firebase": "^5.5.6",
    "react-native-gesture-handler": "^1.3.0",
    "react-native-gesture-responder": "^0.1.1",
    "react-native-google-signin": "^2.0.0",
    "react-native-image-crop-picker": "^0.26.1",
    "react-native-inappbrowser-reborn": "^3.3.3",
    "react-native-keyboard-aware-scroll-view": "^0.8.0",
    "react-native-largelist-v3": "^3.0.15",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-loading-spinner-overlay": "^1.0.1",
    "react-native-localize": "^1.3.1",
    "react-native-maps": "0.26.1",
    "react-native-permissions": "^1.2.0",
    "react-native-phone-input": "^0.2.4",
    "react-native-popup-menu": "^0.15.6",
    "react-native-reanimated": "^1.1.0",
    "react-native-spring-scrollview": "^2.0.23",
    "react-native-svg": "^9.6.2",
    "react-native-svg-uri": "^1.2.3",
    "react-native-vector-icons": "^6.6.0",
    "react-native-version-number": "^0.3.6",
    "react-navigation": "^3.11.1",
    "react-redux": "^7.1.0",
    "redux": "^4.0.4",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "rn-round-checkbox": "^0.3.4",
    "toggle-switch-react-native": "^2.1.0",
    "uuid": "^3.3.3"
  },
  "devDependencies": {
    "@babel/core": "7.5.5",
    "@babel/runtime": "7.5.5",
    "@react-native-community/eslint-config": "0.0.3",
    "babel-jest": "24.8.0",
    "eslint": "6.1.0",
    "jest": "24.8.0",
    "jetifier": "^1.6.5",
    "metro-react-native-babel-preset": "0.54.1",
    "react-test-renderer": "16.8.6"
  },
  "jest": {
    "preset": "react-native"
  }
}

This is a project I'm picking up mid-way through and now you've requested the AndroidManifest, I can see theres some commented code relating to firebase. I'll uncomment and get back to you if I get it working

wfhm commented 4 years ago

@ymcewen,

Thank you for the clarification. From what I could find, it seems more like a warning from react-native-firebase rather than from Pushwoosh plugin. There might've been a conflict between FirebaseMessagingService classes added by both plugins, however I cannot say for sure without more information.

Also, were push notifications working as expected with this warning?

ymcewen commented 4 years ago

@wfhm no the push notification didn't come through. It was just the warning. I can confirm that I uncommented the code in the AndroidManifest.xml and I still didn't get the notification.

Would you advise me to remove this


    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>
  <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />```

We are(should) only be using Pushwoosh for notifications.
wfhm commented 4 years ago

@ymcewen,

The better solution would be creating a master FirebaseMessagingService, which will handle push received events and route it to Pushwoosh and Firebase FirebaseMessagingServices depending on push payload.

First, you have to create a new class inside the Pushwoosh plugin package:

package com.pushwoosh.reactnativeplugin;

 import android.content.Context;

 import com.google.firebase.messaging.RemoteMessage;
 import com.pushwoosh.PushwooshFcmHelper;

 import javax.annotation.Nullable;

public class PWFcmHelper extends PushwooshFcmHelper{
 public static void onTokenRefresh(Context context, @Nullable String token){
 super.onTokenRefresh(context, token);
 }
 public static boolean onMessageReceived(Context context, RemoteMessage message){
 super.onMessageReceived(context, message);
 }

 public static boolean isPushwooshMessage(RemoteMessage message){
 super.isPushwooshMessage(message);
 }
}

After that, you need to create a master FirebaseMessagingService class in your main package and use the PWFcmHelper class instead of the PushwooshFcmHelper:

import com.pushwoosh.reactnativeplugin.PWFcmHelper;

public class MainMessagingService extends FirebaseMessagingService {
 private static final String _TAG_ = "MainMessagingService";

 @Override
 public void onMessageReceived(RemoteMessage remoteMessage) {

 if (PWFcmHelper.isPushwooshMessage(remoteMessage)) {
 //this is a Pushwoosh push, SDK will handle it automatically
 PWFcmHelper.onMessageReceived(this, remoteMessage);
 }else {
 //super.onMessageReceived(remoteMessage);
 //or your custom Firebase message handling logic
 }
 }
}

Register this service in the AndroidManifest.xml with the highest priority:

        <service android:name="com.your.packagename.MainMessagingService">
            <intent-filter android:priority="500">
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
ymcewen commented 4 years ago

Thanks, will give this a shot and feedback

wfhm commented 4 years ago

@ymceven,

Any luck so far?

ymcewen commented 4 years ago

To be honest, I got a bit lost with where to add what as I’m not familiar with java or the structure of Rn plugins.

What I did do though was remove all firebase messaging references in my code and this worked.

On 27 Jan 2020, at 8:41 am, wfhm notifications@github.com wrote:

 @ymceven,

Any luck so far?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.