Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 44cb230

Browse files
authored
Merge pull request #717 from avencat/feature/firebase
Add Firebase (FCM) support
2 parents 5318359 + 9ebe403 commit 44cb230

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ React Native Local and Remote Notifications for iOS and Android
1515

1616

1717
## Installation
18-
`npm install --save react-native-push-notification`
18+
`npm install --save react-native-push-notification` or `yarn add react-native-push-notification`
1919

2020
`react-native link`
2121

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

3737
## Android manual Installation
3838

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

4141
In your `android/build.gradle`
4242
```gradle
4343
ext {
4444
googlePlayServicesVersion = "<Your play services version>" // default: "+"
45+
firebaseVersion = "<Your Firebase version>" // default: "+"
4546
4647
// Other settings
4748
compileSdkVersion = <Your compile SDK version> // default: 23
@@ -56,15 +57,19 @@ ext {
5657
In your `AndroidManifest.xml`
5758
```xml
5859
.....
60+
<!-- <Only if you're using GCM> -->
5961
<uses-permission android:name="android.permission.WAKE_LOCK" />
6062
<permission
6163
android:name="${applicationId}.permission.C2D_MESSAGE"
6264
android:protectionLevel="signature" />
6365
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
66+
<!-- </Only if you're using GCM> -->
67+
6468
<uses-permission android:name="android.permission.VIBRATE" />
6569
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
6670

6771
<application ....>
72+
<!-- <Only if you're using GCM> -->
6873
<receiver
6974
android:name="com.google.android.gms.gcm.GcmReceiver"
7075
android:exported="true"
@@ -74,6 +79,7 @@ In your `AndroidManifest.xml`
7479
<category android:name="${applicationId}" />
7580
</intent-filter>
7681
</receiver>
82+
<!-- </Only if you're using GCM> -->
7783

7884
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
7985
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
@@ -86,7 +92,13 @@ In your `AndroidManifest.xml`
8692
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
8793
android:exported="false" >
8894
<intent-filter>
95+
<!-- <Only if you're using GCM> -->
8996
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
97+
<!-- </Only if you're using GCM> -->
98+
99+
<!-- <Else> -->
100+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
101+
<!-- </Else> -->
90102
</intent-filter>
91103
</service>
92104
.....
@@ -149,8 +161,8 @@ PushNotification.configure({
149161
notification.finish(PushNotificationIOS.FetchResult.NoData);
150162
},
151163

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

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

297309
### 1) Specify notification actions for a notification
298-
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.
310+
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.
299311

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

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

351+
## Android Only Methods
352+
353+
`PushNotification.subscribeToTopic(topic: string)` Subscribe to a topic (works only with Firebase)
354+
339355
## Checking Notification Permissions
340356
`PushNotification.checkPermissions(callback: Function)` Check permissions
341357

android/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def DEFAULT_BUILD_TOOLS_VERSION = "23.0.1"
2020
def DEFAULT_TARGET_SDK_VERSION = 23
2121
def DEFAULT_SUPPORT_LIB_VERSION = "23.1.1"
2222
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "+"
23+
def DEFAULT_FIREBASE_MESSAGING_VERSION = "+"
2324

2425
android {
2526
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
@@ -45,11 +46,13 @@ android {
4546
dependencies {
4647
def supportLibVersion = project.hasProperty('supportLibVersion') ? project.supportLibVersion : DEFAULT_SUPPORT_LIB_VERSION
4748
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION
49+
def firebaseVersion = project.hasProperty('firebaseVersion') ? project.firebaseVersion : DEFAULT_FIREBASE_MESSAGING_VERSION
4850

4951
compile fileTree(dir: 'libs', include: ['*.jar'])
5052
testCompile 'junit:junit:4.12'
5153
compile "com.android.support:appcompat-v7:$supportLibVersion"
5254
compile 'com.facebook.react:react-native:+'
5355
compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"
5456
compile 'me.leolin:ShortcutBadger:1.1.8@aar'
57+
compile "com.google.firebase:firebase-messaging:$firebaseVersion"
5558
}

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.Map;
2727
import java.util.Random;
2828

29+
import com.google.firebase.messaging.FirebaseMessaging;
30+
2931
public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener {
3032
public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag
3133

@@ -123,6 +125,11 @@ public void requestPermissions(String senderID) {
123125
reactContext.startService(GCMService);
124126
}
125127

128+
@ReactMethod
129+
public void subscribeToTopic(String topic) {
130+
FirebaseMessaging.getInstance().subscribeToTopic(topic);
131+
}
132+
126133
@ReactMethod
127134
public void presentLocalNotification(ReadableMap details) {
128135
Bundle bundle = Arguments.toBundle(details);

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationListenerService.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.dieam.reactnativepushnotification.modules;
22

3+
import java.util.Map;
4+
import com.google.firebase.messaging.FirebaseMessagingService;
5+
import com.google.firebase.messaging.RemoteMessage;
6+
37
import android.app.ActivityManager;
48
import android.app.ActivityManager.RunningAppProcessInfo;
59
import android.app.Application;
@@ -13,7 +17,6 @@
1317
import com.facebook.react.ReactInstanceManager;
1418
import com.facebook.react.bridge.ReactApplicationContext;
1519
import com.facebook.react.bridge.ReactContext;
16-
import com.google.android.gms.gcm.GcmListenerService;
1720

1821
import org.json.JSONObject;
1922

@@ -22,10 +25,16 @@
2225

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

25-
public class RNPushNotificationListenerService extends GcmListenerService {
28+
public class RNPushNotificationListenerService extends FirebaseMessagingService {
2629

2730
@Override
28-
public void onMessageReceived(String from, final Bundle bundle) {
31+
public void onMessageReceived(RemoteMessage message) {
32+
String from = message.getFrom();
33+
34+
final Bundle bundle = new Bundle();
35+
for(Map.Entry<String, String> entry : message.getData().entrySet()) {
36+
bundle.putString(entry.getKey(), entry.getValue());
37+
}
2938
JSONObject data = getPushData(bundle.getString("data"));
3039
// Copy `twi_body` to `message` to support Twilio
3140
if (bundle.containsKey("twi_body")) {
@@ -112,11 +121,9 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
112121

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

115-
if (!isForeground) {
116-
Application applicationContext = (Application) context.getApplicationContext();
117-
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
118-
pushNotificationHelper.sendToNotificationCentre(bundle);
119-
}
124+
Application applicationContext = (Application) context.getApplicationContext();
125+
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
126+
pushNotificationHelper.sendToNotificationCentre(bundle);
120127
}
121128

122129
private boolean isApplicationInForeground() {

component/index.android.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ NotificationsComponent.prototype.requestPermissions = function(senderID: string)
3030
RNPushNotification.requestPermissions(senderID);
3131
};
3232

33+
NotificationsComponent.prototype.subscribeToTopic = function(topic: string) {
34+
RNPushNotification.subscribeToTopic(topic);
35+
};
36+
3337
NotificationsComponent.prototype.cancelLocalNotifications = function(details: Object) {
3438
RNPushNotification.cancelLocalNotifications(details);
3539
};
@@ -116,4 +120,3 @@ module.exports = {
116120
state: false,
117121
component: new NotificationsComponent()
118122
};
119-

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ Notifications.requestPermissions = function() {
275275
};
276276

277277
/* Fallback functions */
278+
Notifications.subscribeToTopic = function() {
279+
return this.callNative('subscribeToTopic', arguments);
280+
};
281+
278282
Notifications.presentLocalNotification = function() {
279283
return this.callNative('presentLocalNotification', arguments);
280284
};

0 commit comments

Comments
 (0)