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

Commit a913f84

Browse files
author
Axel Vencatareddy
committed
Add Firebase support
1 parent 72aa374 commit a913f84

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-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
@@ -54,15 +55,19 @@ ext {
5455
In your `AndroidManifest.xml`
5556
```xml
5657
.....
58+
<!-- <Only if you're using GCM> -->
5759
<uses-permission android:name="android.permission.WAKE_LOCK" />
5860
<permission
5961
android:name="${applicationId}.permission.C2D_MESSAGE"
6062
android:protectionLevel="signature" />
6163
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
64+
<!-- </Only if you're using GCM> -->
65+
6266
<uses-permission android:name="android.permission.VIBRATE" />
6367
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
6468

6569
<application ....>
70+
<!-- <Only if you're using GCM> -->
6671
<receiver
6772
android:name="com.google.android.gms.gcm.GcmReceiver"
6873
android:exported="true"
@@ -72,6 +77,7 @@ In your `AndroidManifest.xml`
7277
<category android:name="${applicationId}" />
7378
</intent-filter>
7479
</receiver>
80+
<!-- </Only if you're using GCM> -->
7581

7682
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
7783
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
@@ -84,7 +90,13 @@ In your `AndroidManifest.xml`
8490
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
8591
android:exported="false" >
8692
<intent-filter>
93+
<!-- <Only if you're using GCM> -->
8794
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
95+
<!-- </Only if you're using GCM> -->
96+
97+
<!-- <Else> -->
98+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
99+
<!-- </Else> -->
88100
</intent-filter>
89101
</service>
90102
.....
@@ -147,8 +159,8 @@ PushNotification.configure({
147159
notification.finish(PushNotificationIOS.FetchResult.NoData);
148160
},
149161

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

153165
// IOS ONLY (optional): default: all - Permissions to register.
154166
permissions: {
@@ -293,7 +305,7 @@ Property `repeatType` could be one of `week`, `day`, `hour`, `minute`, `time`. I
293305
Two things are required to setup notification actions.
294306

295307
### 1) Specify notification actions for a notification
296-
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.
308+
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.
297309

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

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

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

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public void requestPermissions(String senderID) {
123123
reactContext.startService(GCMService);
124124
}
125125

126+
@ReactMethod
127+
public void subscribeToTopic(String topic) {
128+
FirebaseMessaging.getInstance().subscribeToTopic(topic);
129+
}
130+
126131
@ReactMethod
127132
public void presentLocalNotification(ReadableMap details) {
128133
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
if (data != null) {
3140
if (!bundle.containsKey("message")) {
@@ -107,11 +116,9 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
107116

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

110-
if (!isForeground) {
111-
Application applicationContext = (Application) context.getApplicationContext();
112-
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
113-
pushNotificationHelper.sendToNotificationCentre(bundle);
114-
}
119+
Application applicationContext = (Application) context.getApplicationContext();
120+
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
121+
pushNotificationHelper.sendToNotificationCentre(bundle);
115122
}
116123

117124
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-

0 commit comments

Comments
 (0)