flurry / react-native-flurry-sdk

React Native Flurry SDK
Apache License 2.0
45 stars 13 forks source link

Push Notification Campaign doesn't work on Android #38

Closed leonnardovv closed 3 years ago

leonnardovv commented 3 years ago

Describe the bug The push notifications campaign doesn't work on Android, even if the test notification works when we send it with push token before starting the campaign. On iOS everything works fine.

This Bug is About

Platform

Environment

To Reproduce Steps to reproduce the behavior:

  1. Add flurry manual/automate configuration
  2. Create a campaign
  3. Test the notification with Android push token (here it works)
  4. Start the campaign (the messages are never sent/received)

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Are you using Flurry Push/Messaging? Yes

Add any other context about the problem here.

This is the MainApplication.java


package com.myspecificapp.app

import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.flurry.android.reactnative.FlurryModule;
import com.flurry.android.reactnative.FlurryPackage;
import com.flurry.android.FlurryAgent;
import com.flurry.android.marketing.FlurryMarketingModule;
import com.flurry.android.marketing.messaging.FlurryMessaging;
import com.flurry.android.marketing.messaging.FlurryMessagingListener;
import com.flurry.android.marketing.messaging.notification.FlurryMessage;
import com.flurry.android.reactnative.FlurryPackage;
import com.flurry.android.reactnative.FlurryPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.myspecificapp.app.generated.BasePackageList;
import org.unimodules.adapters.react.ReactAdapterPackage;

import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;
import org.unimodules.core.interfaces.Package;
import org.unimodules.core.interfaces.SingletonModule;

import expo.modules.constants.ConstantsPackage;
import expo.modules.permissions.PermissionsPackage;
import expo.modules.filesystem.FileSystemPackage;
import expo.modules.updates.UpdatesController;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Nullable;
import com.facebook.react.bridge.JSIModulePackage;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.internal.FirebaseInstanceIdInternal;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;

import com.flurry.android.marketing.FlurryMarketingOptions;
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

import androidx.multidex.MultiDexApplication;

public class MainApplication extends MultiDexApplication implements ReactApplication {
    private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(
            new BasePackageList().getPackageList()
    );

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {

        @Override
        protected String getJSMainModuleName() {
            return "index";
        }

        @Override
        protected JSIModulePackage getJSIModulePackage() {
            return new ReanimatedJSIModulePackage(); 
        }

        @Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            List<ReactPackage> packages = new PackageList(this).getPackages();
            new ReactNativePushNotificationPackage(); 
            packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider));
            return packages;
        }

        @Override
        protected @Nullable
        String getJSBundleFile() {
            if (BuildConfig.DEBUG) {
                return super.getJSBundleFile();
            } else {
                return UpdatesController.getInstance().getLaunchAssetFile();
            }
        }

        @Override
        protected @Nullable
        String getBundleAssetName() {
            if (BuildConfig.DEBUG) {
                return super.getBundleAssetName();
            } else {
                return UpdatesController.getInstance().getBundleAssetName();
            }
        }
    };

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        String refreshedToken =  MyFirebaseMessagingService.getToken(this);

        FlurryMessagingListener myFlurryMessagingListener = new MyFlurryMessagingListener(this);

        FlurryMarketingOptions flurryMessagingOptions = new FlurryMarketingOptions.Builder()
                .setupMessagingWithAutoIntegration()
                .withDefaultNotificationChannelId(String.valueOf(R.string.default_notification_channel_id))
                .withDefaultNotificationIconResourceId(R.drawable.ic_stat_ic_notification)
                .withDefaultNotificationIconAccentColor(getResources().getColor(R.color.splashscreen_bg))
                .withFlurryMessagingListener(myFlurryMessagingListener)
                .build();

        FlurryMarketingModule marketingModule = new FlurryMarketingModule(flurryMessagingOptions);

        new FlurryAgent.Builder()
                .withModule(marketingModule)
                .withIncludeBackgroundSessionsInMetrics(true)
                .build(this, "Our specific token");

        FlurryMessaging.setToken(refreshedToken);
        Log.d("Message is: ", "Set token done with: " + refreshedToken);

        FlurryMessaging.setToken(refreshedToken);

        SoLoader.init(this, /* native exopackage */ false);
        initializeFlipper(this); // Remove this line if you don't want Flipper enabled
        if (!BuildConfig.DEBUG) {
            UpdatesController.initialize(this);
        }

    }

    /**
     * Loads Flipper in React Native templates.
     *
     * @param context
     */
    private static void initializeFlipper(Context context) {
        if (BuildConfig.DEBUG) {
            try {
         /*
          We use reflection here to pick up the class that initializes Flipper,
         since Flipper library is not available in release mode
         */
                Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
                aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
leonnardovv commented 3 years ago

Fixed: The issue was from firebase, we had to add: implementation 'com.google.firebase:firebase-iid' to build.gradle