Я использую библиотеку реактив-родной-firebase для push-уведомлений Firebase в React native от Android Platfrom.
Мой код прекрасно работает в отладочном apk, но когда я экспортирую свой apk в режим релиза, 'firebase.messaging (). GetToken ()' не вызывается из моей библиотеки Android-Resact-native-Firebase класс 'RNFirebaseMessaging.java' .
Может кто-нибудь, пожалуйста, подскажите мне, почему я сталкиваюсь с этой проблемой и как решить эту проблему.
Ниже мой код:
MainApplication.java
package com.njrefresh;
import android.app.Application;
import com.RNFetchBlob.RNFetchBlobPackage;
import com.dylanvann.fastimage.FastImageViewPackage;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.github.alinz.reactnativewebviewbridge.WebViewBridgePackage;
import com.github.xinthink.rnmk.ReactMaterialKitPackage;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import com.oblador.vectoricons.VectorIconsPackage;
import org.devio.rn.splashscreen.SplashScreenReactPackage;
import org.wonday.pdf.RCTPdfView;
import java.util.Arrays;
import java.util.List;
import cl.json.RNSharePackage;
import io.invertase.firebase.RNFirebasePackage;
import io.invertase.firebase.analytics.RNFirebaseAnalyticsPackage;
import io.invertase.firebase.instanceid.RNFirebaseInstanceIdPackage;
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNFirebaseNotificationsPackage(),
new RNFirebaseMessagingPackage(),
new RNFirebaseInstanceIdPackage(),
new RNFirebasePackage(),
new ReactMaterialKitPackage(),
new RNSharePackage(),
new WebViewBridgePackage(),
new FastImageViewPackage(),
new VectorIconsPackage(),
new SplashScreenReactPackage(),
new RCTPdfView(),
new RNFetchBlobPackage(),
new RNDeviceInfo(),
new RNFirebaseAnalyticsPackage(),
// new ReactNativePushNotificationPackage(),
new ModulusPackager()
);
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
private FirebaseAnalytics sAnalytics;
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, false);
FirebaseApp.initializeApp(getApplicationContext(), new FirebaseOptions.Builder()
.setProjectId(getString(R.string.fa_project_id))
.setDatabaseUrl(getString(R.string.fa_database_url))
.setApplicationId(getString(R.string.fa_app_id)) // Required for Analytics.
.setApiKey(getString(R.string.fa_api_key)) // Required for Auth.
.build(), FirebaseApp.DEFAULT_APP_NAME);
getDefaultTracker();
FirebaseApp.initializeApp(getApplicationContext(), new FirebaseOptions.Builder()
.setProjectId(getString(R.string.fcm_project_id))
.setDatabaseUrl(getString(R.string.fcm_database_url))
.setApplicationId(getString(R.string.fcm_app_id))
.setApiKey(getString(R.string.fcm_api_key))
.build(), getString(R.string.fcm_instance_name));
}
protected boolean isDefaultFirebaseAppInitialized() {
try {
// try get
return FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME) != null;
// catch illegal state exc
} catch (IllegalStateException ise) {
// on such case not initialized
return false;
}
}
synchronized public FirebaseAnalytics getDefaultTracker() {
if (sAnalytics == null) {
if (isDefaultFirebaseAppInitialized()) {
sAnalytics = FirebaseAnalytics.getInstance(this);
} else {
}
}
return sAnalytics;
}
}
RNFirebaseMessaging.java
package io.invertase.firebase.messaging;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import java.io.IOException;
import javax.annotation.Nonnull;
import io.invertase.firebase.Utils;
public class RNFirebaseMessaging extends ReactContextBaseJavaModule {
private static final String TAG = "RNFirebaseMessaging";
private String token="NA";
RNFirebaseMessaging(ReactApplicationContext context) {
super(context);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
// Subscribe to message events
localBroadcastManager.registerReceiver(
new MessageReceiver(),
new IntentFilter(RNFirebaseMessagingService.MESSAGE_EVENT)
);
// Subscribe to new token events
localBroadcastManager.registerReceiver(
new RefreshTokenReceiver(),
new IntentFilter(RNFirebaseMessagingService.NEW_TOKEN_EVENT)
);
}
@Override
public String getName() {
return "RNFirebaseMessaging";
}
@ReactMethod
public void getToken(Promise promise) {
Log.d(TAG, "HARISH LOG: secondary=>");
try {
FirebaseInstanceId.getInstance(FirebaseApp.getInstance("secondary")).getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
token = task.getResult().getToken();
Log.d(TAG, "onCompleteToken: secondary=>"+token);
}
});
promise.resolve(token);
} catch (Throwable e) {
e.printStackTrace();
promise.reject("messaging/fcm-token-error", e.getMessage());
}
}
@ReactMethod
public void deleteToken(Promise promise) {
try {
String senderId = FirebaseApp.getInstance().getOptions().getGcmSenderId();
FirebaseInstanceId.getInstance().deleteToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE);
promise.resolve(null);
} catch (Throwable e) {
e.printStackTrace();
promise.reject("messaging/fcm-token-error", e.getMessage());
}
}
@ReactMethod
public void requestPermission(Promise promise) {
Log.d(TAG, "requestPermission Harish");
promise.resolve(null);
}
// Non Web SDK methods
@ReactMethod
public void hasPermission(Promise promise) {
Boolean enabled = NotificationManagerCompat
.from(getReactApplicationContext())
.areNotificationsEnabled();
promise.resolve(enabled);
}
@ReactMethod
public void sendMessage(ReadableMap messageMap, Promise promise) {
if (!messageMap.hasKey("to")) {
promise.reject("messaging/invalid-message", "The supplied message is missing a 'to' field");
return;
}
RemoteMessage.Builder mb = new RemoteMessage.Builder(messageMap.getString("to"));
if (messageMap.hasKey("collapseKey")) {
mb = mb.setCollapseKey(messageMap.getString("collapseKey"));
}
if (messageMap.hasKey("messageId")) {
mb = mb.setMessageId(messageMap.getString("messageId"));
}
if (messageMap.hasKey("messageType")) {
mb = mb.setMessageType(messageMap.getString("messageType"));
}
if (messageMap.hasKey("ttl")) {
mb = mb.setTtl(messageMap.getInt("ttl"));
}
if (messageMap.hasKey("data")) {
ReadableMap dataMap = messageMap.getMap("data");
ReadableMapKeySetIterator iterator = dataMap.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
mb = mb.addData(key, dataMap.getString(key));
}
}
FirebaseMessaging.getInstance().send(mb.build());
// TODO: Listen to onMessageSent and onSendError for better feedback?
promise.resolve(null);
}
@ReactMethod
public void subscribeToTopic(String topic, final Promise promise) {
FirebaseMessaging
.getInstance()
.subscribeToTopic(topic)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@Nonnull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "subscribeToTopic:onComplete:success");
promise.resolve(null);
} else {
Exception exception = task.getException();
Log.e(TAG, "subscribeToTopic:onComplete:failure", exception);
promise.reject(exception);
}
}
});
}
@ReactMethod
public void unsubscribeFromTopic(String topic, final Promise promise) {
FirebaseMessaging
.getInstance()
.unsubscribeFromTopic(topic)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@Nonnull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "unsubscribeFromTopic:onComplete:success");
promise.resolve(null);
} else {
Exception exception = task.getException();
Log.e(TAG, "unsubscribeFromTopic:onComplete:failure", exception);
promise.reject(exception);
}
}
});
}
private class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
Log.d(TAG, "Received new message");
RemoteMessage message = intent.getParcelableExtra("message");
WritableMap messageMap = MessagingSerializer.parseRemoteMessage(message);
Utils.sendEvent(getReactApplicationContext(), "messaging_message_received", messageMap);
}
}
}
private class RefreshTokenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
Log.d(TAG, "Received new messaging token.");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
FirebaseInstanceId.getInstance(FirebaseApp.getInstance("secondary")).getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
token = task.getResult().getToken();
Log.d(TAG, "onCompleteToken: =>"+token);
}
});
if (token != null) {
Log.d(TAG, "Sending new messaging token event.");
Utils.sendEvent(getReactApplicationContext(), "messaging_token_refreshed", token);
}
}
});
thread.start();
}
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.njrefresh">
<uses-permission android:name="android.permission.INTERNET" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<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:allowBackup="true"
android:theme="@style/AppTheme"
android:largeHeap="true">
<provider
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:authorities="${applicationId}.firebaseinitprovider"
tools:node="remove" />
<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
<intent-filter android:priority="10" >
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
<receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
<receiver android:enabled="true" android:exported="true" android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
<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>
<activity
android:launchMode="singleTop"
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateAlwaysHidden"
android:exported="true"
android:screenOrientation="portrait"
>
<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>
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService"
android:exported="true" >
<intent-filter android:priority="10" >
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="refresh-channel"/>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
</application>
</manifest>