Skip to content

Commit

Permalink
Merge pull request #717 from avencat/feature/firebase
Browse files Browse the repository at this point in the history
Add Firebase (FCM) support
  • Loading branch information
Gp2mv3 authored Jul 17, 2018
2 parents 5318359 + 9ebe403 commit 44cb230
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ React Native Local and Remote Notifications for iOS and Android


## Installation
`npm install --save react-native-push-notification`
`npm install --save react-native-push-notification` or `yarn add react-native-push-notification`

`react-native link`

Expand All @@ -36,12 +36,13 @@ The component uses PushNotificationIOS for the iOS part.

## Android manual Installation

**NOTE: To use a specific `play-service-gcm` version:**
**NOTE: To use a specific `play-service-gcm` or `firebase-messaging` version:**

In your `android/build.gradle`
```gradle
ext {
googlePlayServicesVersion = "<Your play services version>" // default: "+"
firebaseVersion = "<Your Firebase version>" // default: "+"
// Other settings
compileSdkVersion = <Your compile SDK version> // default: 23
Expand All @@ -56,15 +57,19 @@ ext {
In your `AndroidManifest.xml`
```xml
.....
<!-- <Only if you're using GCM> -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<!-- </Only if you're using GCM> -->

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

<application ....>
<!-- <Only if you're using GCM> -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
Expand All @@ -74,6 +79,7 @@ In your `AndroidManifest.xml`
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<!-- </Only if you're using GCM> -->

<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
Expand All @@ -86,7 +92,13 @@ In your `AndroidManifest.xml`
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<!-- <Only if you're using GCM> -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- </Only if you're using GCM> -->

<!-- <Else> -->
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<!-- </Else> -->
</intent-filter>
</service>
.....
Expand Down Expand Up @@ -149,8 +161,8 @@ PushNotification.configure({
notification.finish(PushNotificationIOS.FetchResult.NoData);
},

// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: "YOUR GCM SENDER ID",
// ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: "YOUR GCM (OR FCM) SENDER ID",

// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
Expand Down Expand Up @@ -295,7 +307,7 @@ Property `repeatType` could be one of `week`, `day`, `hour`, `minute`, `time`. I
Two things are required to setup notification actions.

### 1) Specify notification actions for a notification
This is done by specifying an `actions` parameters while configuring the local notification. This is an array of strings where each string is a notificaiton action that will be presented with the notification.
This is done by specifying an `actions` parameters while configuring the local notification. This is an array of strings where each string is a notification action that will be presented with the notification.

For e.g. `actions: '["Accept", "Reject"]' // Must be in string format`

Expand Down Expand Up @@ -336,6 +348,10 @@ Uses the [ShortcutBadger](https://github.com/leolin310148/ShortcutBadger) on And
## Sending Notification Data From Server
Same parameters as `PushNotification.localNotification()`

## Android Only Methods

`PushNotification.subscribeToTopic(topic: string)` Subscribe to a topic (works only with Firebase)

## Checking Notification Permissions
`PushNotification.checkPermissions(callback: Function)` Check permissions

Expand Down
3 changes: 3 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def DEFAULT_BUILD_TOOLS_VERSION = "23.0.1"
def DEFAULT_TARGET_SDK_VERSION = 23
def DEFAULT_SUPPORT_LIB_VERSION = "23.1.1"
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "+"
def DEFAULT_FIREBASE_MESSAGING_VERSION = "+"

android {
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
Expand All @@ -45,11 +46,13 @@ android {
dependencies {
def supportLibVersion = project.hasProperty('supportLibVersion') ? project.supportLibVersion : DEFAULT_SUPPORT_LIB_VERSION
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION
def firebaseVersion = project.hasProperty('firebaseVersion') ? project.firebaseVersion : DEFAULT_FIREBASE_MESSAGING_VERSION

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile 'com.facebook.react:react-native:+'
compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"
compile 'me.leolin:ShortcutBadger:1.1.8@aar'
compile "com.google.firebase:firebase-messaging:$firebaseVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Map;
import java.util.Random;

import com.google.firebase.messaging.FirebaseMessaging;

public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener {
public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag

Expand Down Expand Up @@ -123,6 +125,11 @@ public void requestPermissions(String senderID) {
reactContext.startService(GCMService);
}

@ReactMethod
public void subscribeToTopic(String topic) {
FirebaseMessaging.getInstance().subscribeToTopic(topic);
}

@ReactMethod
public void presentLocalNotification(ReadableMap details) {
Bundle bundle = Arguments.toBundle(details);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.dieam.reactnativepushnotification.modules;

import java.util.Map;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Application;
Expand All @@ -13,7 +17,6 @@
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.google.android.gms.gcm.GcmListenerService;

import org.json.JSONObject;

Expand All @@ -22,10 +25,16 @@

import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;

public class RNPushNotificationListenerService extends GcmListenerService {
public class RNPushNotificationListenerService extends FirebaseMessagingService {

@Override
public void onMessageReceived(String from, final Bundle bundle) {
public void onMessageReceived(RemoteMessage message) {
String from = message.getFrom();

final Bundle bundle = new Bundle();
for(Map.Entry<String, String> entry : message.getData().entrySet()) {
bundle.putString(entry.getKey(), entry.getValue());
}
JSONObject data = getPushData(bundle.getString("data"));
// Copy `twi_body` to `message` to support Twilio
if (bundle.containsKey("twi_body")) {
Expand Down Expand Up @@ -112,11 +121,9 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl

Log.v(LOG_TAG, "sendNotification: " + bundle);

if (!isForeground) {
Application applicationContext = (Application) context.getApplicationContext();
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
pushNotificationHelper.sendToNotificationCentre(bundle);
}
Application applicationContext = (Application) context.getApplicationContext();
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
pushNotificationHelper.sendToNotificationCentre(bundle);
}

private boolean isApplicationInForeground() {
Expand Down
5 changes: 4 additions & 1 deletion component/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ NotificationsComponent.prototype.requestPermissions = function(senderID: string)
RNPushNotification.requestPermissions(senderID);
};

NotificationsComponent.prototype.subscribeToTopic = function(topic: string) {
RNPushNotification.subscribeToTopic(topic);
};

NotificationsComponent.prototype.cancelLocalNotifications = function(details: Object) {
RNPushNotification.cancelLocalNotifications(details);
};
Expand Down Expand Up @@ -116,4 +120,3 @@ module.exports = {
state: false,
component: new NotificationsComponent()
};

4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ Notifications.requestPermissions = function() {
};

/* Fallback functions */
Notifications.subscribeToTopic = function() {
return this.callNative('subscribeToTopic', arguments);
};

Notifications.presentLocalNotification = function() {
return this.callNative('presentLocalNotification', arguments);
};
Expand Down

0 comments on commit 44cb230

Please sign in to comment.