Skip to content
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
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ Edit `AppDelegate.m`:
+ handler(UIBackgroundFetchResultNewData);
+ }

+// Required to register for notifications
+- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
+{
+ [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
+}

+// Required for the localNotification event.
+- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
+{
Expand All @@ -177,6 +171,9 @@ class App extends Component {
this.notificationUnsubscribe = FCM.on('notification', (notif) => {
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
});
this.localNotificationUnsubscribe = FCM.on('localNotification', (notif) => {
// notif.notification contains the data
});
this.refreshUnsubscribe = FCM.on('refreshToken', (token) => {
console.log(token)
// fcm token may not be available on first load, catch it here
Expand All @@ -190,6 +187,7 @@ class App extends Component {
// prevent leaking
this.refreshUnsubscribe();
this.notificationUnsubscribe();
this.localNotificationUnsubscribe();
}
}
```
Expand Down Expand Up @@ -255,7 +253,6 @@ Based on react-native-push-notification by zo0r and Neson
//support for priority on android
const pushNotification = {
/* Android Only Properties */
id: 0, // (optional) default: Autogenerated Unique ID
priority: "high", //(optional) default: low, other options: 'min', 'max', 'high'
title: "My Notification Title", // (optional)
ticker: "My Notification Ticker", // (optional)
Expand All @@ -271,28 +268,26 @@ Based on react-native-push-notification by zo0r and Neson
group: "group", // (optional) add group to message
type:'my_custom_field_value', //custom field
/* iOS and Android properties */
id: 1234, // (optional)
message: "My Notification Message", // (required)
playSound: false, // (optional) default: true
number: 10 // (optional) default: none (Cannot be zero)
};

FCM.presentLocalNotification(pushNotification)

//Schedule Local Notification
PushNotificationIOS will add support for this https://github.com/facebook/react-native/pull/7219 so this will change

const pushNotification = {
//...
repeatEvery: "week"// other options: minute, hour, day, week
//...
}
FCM.scheduleLocalNotification(pushNotification)

//Cancel notifications(IOS and ANDROID)
FCM.cancelAll()
//Cancel all notifications(IOS and ANDROID)
FCM.cancelLocalNotifications()

//Android Only
FCM.cancel(notificationId)
//Cancel notification by id(IOS and ANDROID)
FCM.cancelLocalNotification(notificationID)
```

## Q & A
Expand Down
Binary file added android/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ private AlarmManager getAlarmManager() {
private PendingIntent getScheduleNotificationIntent(Bundle bundle, Boolean storeIntent) {

int notificationID = (int) System.currentTimeMillis();

if (bundle.containsKey("id")) {
try {
notificationID = (int) bundle.getDouble("id");
} catch (Exception e) {
String notificationIDString = bundle.getString("id");
notificationID = (int) bundle.getDouble("id");
Log.w(TAG, String.valueOf(notificationID));

if(notificationID == 0){
String notificationIDString = bundle.getString("id");
if (notificationIDString != null) {
Log.w(TAG, "'id' field set as a string instead of an int");

Expand Down Expand Up @@ -110,7 +111,7 @@ public void sendNotification(Bundle bundle) {


String priority = bundle.getString("priority");
switch(priority) {
switch(priority==null?"":priority) {
case "min":
notification.setPriority(NotificationCompat.PRIORITY_MIN);
break;
Expand Down Expand Up @@ -214,12 +215,12 @@ public void sendNotification(Bundle bundle) {
}

int notificationID = (int) System.currentTimeMillis();

if (bundle.containsKey("id")) {
try {
notificationID = (int) bundle.getDouble("id");
} catch (Exception e) {
String notificationIDString = bundle.getString("id");
notificationID = (int) bundle.getDouble("id");

if(notificationID == 0){
String notificationIDString = bundle.getString("id");
if (notificationIDString != null) {
Log.w(TAG, "'id' field set as a string instead of an int");

Expand All @@ -232,6 +233,7 @@ public void sendNotification(Bundle bundle) {
}
}

intent.putExtra("localNotification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

Expand Down Expand Up @@ -314,14 +316,14 @@ public void sendNotificationScheduled(Bundle bundle) {

}

public void cancel(Integer notificationID) {
public void cancelLocalNotification(double notificationID) {
NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(notificationID);
notificationManager.cancel((int) notificationID);

cancelAlarm(Integer.toString(notificationID));
deleteFromPreferences(Integer.toString(notificationID));
cancelAlarm(notificationID);
deleteFromPreferences(Double.toString(notificationID));
}

public void deleteFromPreferences(String id) {
Expand All @@ -330,7 +332,7 @@ public void deleteFromPreferences(String id) {
editor.commit();
}

public void cancelAll() {
public void cancelLocalNotifications() {
NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

Expand All @@ -339,17 +341,17 @@ public void cancelAll() {
cancelAlarms();
}

public void cancelAlarm(String notificationID) {
public void cancelAlarm(double notificationID) {
Bundle b = new Bundle();
b.putString("id", notificationID);
b.putDouble("id", notificationID);
getAlarmManager().cancel(getScheduleNotificationIntent( b, false));
}

public void cancelAlarms() {
java.util.Map<String, ?> keyMap = sharedPreferences.getAll();
SharedPreferences.Editor editor = sharedPreferences.edit();
for(java.util.Map.Entry<String, ?> entry:keyMap.entrySet()){
cancelAlarm(entry.getKey());
cancelAlarm(Double.parseDouble(entry.getKey()));
}
editor.clear();
editor.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public void scheduleLocalNotification(ReadableMap details) {
}

@ReactMethod
public void cancel(Integer notificationID) {
mFIRLocalMessagingHelper.cancel(notificationID);
public void cancelLocalNotification(int notificationID) {
mFIRLocalMessagingHelper.cancelLocalNotification(notificationID);
}
@ReactMethod
public void cancelAll() {
mFIRLocalMessagingHelper.cancelAll();
public void cancelLocalNotifications() {
mFIRLocalMessagingHelper.cancelLocalNotifications();
}

@ReactMethod
Expand Down Expand Up @@ -183,6 +183,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

@Override
public void onNewIntent(Intent intent){
sendEvent("FCMNotificationReceived", parseIntent(intent));
Bundle bundle = intent.getExtras();
Boolean isLocalNotification = bundle.getBoolean("localNotification", false);
sendEvent(isLocalNotification ? "LocalNotificationReceived" : "FCMNotificationReceived", parseIntent(intent));
}
}
36 changes: 13 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {NativeModules, DeviceEventEmitter, Platform, PushNotificationIOS} from 'react-native';
import {NativeModules, DeviceEventEmitter, Platform} from 'react-native';

const eventsMap = {
refreshToken: 'FCMTokenRefreshed',
notification: 'FCMNotificationReceived'
notification: 'FCMNotificationReceived',
localNotification: 'LocalNotificationReceived'
};

const REPEAT_INTERVAL_IOS = {
Expand Down Expand Up @@ -33,7 +34,7 @@ FCM.presentLocalNotification = (details) =>{

else if (Platform.OS ==='ios') {
const soundName = !details.hasOwnProperty("playSound") || details.playSound === true ? 'default' : '';// empty string results in no sound
PushNotificationIOS.presentLocalNotification({
FIRMessaging.presentLocalNotification({
alertBody: details.message,
alertAction: details.alertAction,
category: details.category,
Expand All @@ -48,43 +49,32 @@ FCM.scheduleLocalNotification = function(details) {
var iosNotification = {
fireDate: details.date.getTime(),
alertBody: details.message,
userInfo: details.userInfo,
userInfo: details.userInfo || {},
repeatInterval: REPEAT_INTERVAL_IOS[details.repeatEvery] || 0
};
if(details.id) {
iosNotification.userInfo.id = details.id;
}
details.fireDate = details.date.getTime();
delete details.date;

FIRMessaging.scheduleLocalNotification((Platform.OS === 'ios')? iosNotification : details);
};

FCM.cancel = (notificationID) => {
if(Platform.OS === 'android'){
FIRMessaging.cancel(notificationID);
}
FCM.cancelLocalNotification = (notificationID) => {
FIRMessaging.cancelLocalNotification(notificationID);
};

FCM.cancelAll = () => {
if (Platform.OS ==='android'){
FIRMessaging.cancelAll();
}
else if (Platform.OS ==='ios') {
PushNotificationIOS.cancelLocalNotifications();
PushNotificationIOS.setApplicationIconBadgeNumber(0);
}
FCM.cancelLocalNotifications = () => {
FIRMessaging.cancelLocalNotifications();
};

FCM.on = (event, callback) => {
const nativeEvent = eventsMap[event];

if(Platform.OS === 'ios'){
PushNotificationIOS.addEventListener('localNotification', callback);
}
const listener = DeviceEventEmitter.addListener(nativeEvent, callback);

return function remove() {
listener.remove();
if(Platform.OS === 'ios'){
PushNotificationIOS.removeEventListener('localNotification', callback);
}
};
};

Expand Down
50 changes: 40 additions & 10 deletions ios/RNFIRMesssaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#endif

NSString *const FCMNotificationReceived = @"FCMNotificationReceived";
NSString *const LocalNotificationReceived = @"LocalNotificationReceived";

@implementation RCTConvert (UILocalNotification)

Expand All @@ -29,12 +30,9 @@ + (UILocalNotification *)UILocalNotification:(id)json
notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName;
notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]];
notification.category = [RCTConvert NSString:details[@"category"]];

NSNumber *repeatInterval = details[@"repeatInterval"];

if (repeatInterval) {
notification.repeatInterval = [RCTConvert NSInteger:repeatInterval];
}
if (details[@"repeatInterval"]) {
notification.repeatInterval = [RCTConvert NSInteger:details[@"repeatInterval"]];
}

return notification;
}
Expand Down Expand Up @@ -75,6 +73,11 @@ - (void)setBridge:(RCTBridge *)bridge
selector:@selector(connectToFCM)
name:UIApplicationDidBecomeActiveNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLocalNotificationReceived:)
name:LocalNotificationReceived
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(onTokenRefresh)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
Expand Down Expand Up @@ -129,19 +132,46 @@ - (void) onTokenRefresh
}
}

RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic)
{
[[FIRMessaging messaging] subscribeToTopic:topic];
}

RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic)
{
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
}

RCT_EXPORT_METHOD(presentLocalNotification:(UILocalNotification *)notification)
{
[RCTSharedApplication() presentLocalNotificationNow:notification];
}

RCT_EXPORT_METHOD(scheduleLocalNotification:(UILocalNotification *)notification)
{
[RCTSharedApplication() scheduleLocalNotification:notification];
}

RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic)
RCT_EXPORT_METHOD(cancelLocalNotifications)
{
[[FIRMessaging messaging] subscribeToTopic:topic];
[RCTSharedApplication() cancelAllLocalNotifications];
}

RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic)
RCT_EXPORT_METHOD(cancelLocalNotification:(NSInteger*) notificationId)
{
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) {
NSDictionary<NSString *, id> *notificationInfo = notification.userInfo;
int getId=[[notificationInfo valueForKey:@"id"] intValue];
if(getId == notificationId){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}

- (void)handleLocalNotificationReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:LocalNotificationReceived
body:notification.userInfo];
}

- (void)handleRemoteNotificationReceived:(NSNotification *)notification
Expand Down