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

fix: firebase push notification #101

Merged
merged 6 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .watchmanconfig

This file was deleted.

39 changes: 25 additions & 14 deletions App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { BackHandler, Alert } from "react-native";
import { BackHandler, Alert, AppRegistry } from "react-native";
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Provider } from "react-redux";
Expand All @@ -13,16 +13,26 @@ import AboutScreen from "./screens/About";
import ReaderScreen from "./screens/Reader";
import BookmarksScreen from "./screens/Bookmarks";
import createStore from "./config/store";
import FirebaseNotification from "./utils/firebaseNotification";

const Stack = createNativeStackNavigator();

const { store, persistor } = createStore();

export default class App extends React.Component {
componentDidMount() {
this.notificationHandler();
BackHandler.addEventListener("hardwareBackPress", this.handleBackPress);
}

notificationHandler() {
const firebaseNotifaction = new FirebaseNotification()
firebaseNotifaction.checkPermission();
firebaseNotifaction.backgroundMessageHandler();
firebaseNotifaction.foregroundMessage();
firebaseNotifaction.handleNotification();
}

componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this.handleBackPress);
}
Expand All @@ -45,22 +55,23 @@ export default class App extends React.Component {
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerShown: false
}}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="FolderBani" component={FolderBaniScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
<Stack.Screen name="Reader" component={ReaderScreen} />
<Stack.Screen name="Bookmarks" component={BookmarksScreen} />
<Stack.Screen name="EditBaniOrder" component={EditBaniOrderScreen} />
<Stack.Screen name="ReminderOptions" component={ReminderOptionsScreen} />
<Stack.Screen name="About" component={AboutScreen} />
</Stack.Navigator>
<Stack.Navigator
screenOptions={{
headerShown: false
}}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="FolderBani" component={FolderBaniScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
<Stack.Screen name="Reader" component={ReaderScreen} />
<Stack.Screen name="Bookmarks" component={BookmarksScreen} />
<Stack.Screen name="EditBaniOrder" component={EditBaniOrderScreen} />
<Stack.Screen name="ReminderOptions" component={ReminderOptionsScreen} />
<Stack.Screen name="About" component={AboutScreen} />
</Stack.Navigator>
</NavigationContainer>
</PersistGate>
</Provider>
);
}
}
AppRegistry.registerHeadlessTask('RNFirebaseMessagingService', () => notificationHandler);
8 changes: 8 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

<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|uiMode"
android:launchMode="singleTask" android:windowSoftInputMode="adjustResize">
<activity android:name=".SplashActivity" android:theme="@style/SplashTheme" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".MainActivity" android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask" android:windowSoftInputMode="adjustResize">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.WahegurooNetwork.SundarGutka;

import android.app.Application;
import android.content.Context;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

Expand Down Expand Up @@ -41,14 +38,12 @@ protected String getJSMainModuleName() {
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}

@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
}

/**
* Loads Flipper in React Native templates. Call this in the onCreate method with something like
* initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.WahegurooNetwork.SundarGutka;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.WahegurooNetwork.SundarGutka.Notification.Service.NotificationService;
import com.facebook.react.HeadlessJsTaskService;

import java.util.List;


public class NetworkChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, final Intent intent) {
/**
This part will be called every time network connection is changed
e.g. Connected -> Not Connected
**/
Log.d("Notification","Notifications");
if (!isAppOnForeground((context))) {
/**
We will start our service and send extra info about
network connections
**/
boolean hasInternet = isNetworkAvailable(context);
Intent serviceIntent = new Intent(context, NotificationService.class);
serviceIntent.putExtra("hasInternet", hasInternet);
context.startService(serviceIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
}
}

private boolean isAppOnForeground(Context context) {
/**
We need to check if app is in foreground otherwise the app will crash.
http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not
**/
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses =
activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance ==
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}

public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return (netInfo != null && netInfo.isConnected());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.WahegurooNetwork.SundarGutka.Notification.Service;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;
//creating the service class
public class NotificationService extends HeadlessJsTaskService {
@Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
return new HeadlessJsTaskConfig(
"NotificationHandler", //JS function to call
extras != null ? Arguments.fromBundle(extras) : null,
5000,
true);
}
}
Loading