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

Added method to control push notification badge count in iOS. #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ const handleNotification = notification => {
// You can fetch required data here don't do anything with UI
case 'active':
// App is foreground and notification is received. Show a alert or something.

// You can also decrease or clear badge count with the help of below methods.
// For now those works on iOS only as for Android we have not added any functionality yet.
// Contributors can help here if they want from Android community.
RNPusherPushNotifications.getApplicationIconBadgeNumber((badgeCount) => {
if (badgeCount > 0) {
RNPusherPushNotifications.setApplicationIconBadgeNumber(badgeCount - 1);
}
});
default:
break;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,13 @@ public void run() {
});
}

@ReactMethod
public void getApplicationIconBadgeNumber(final Callback callback) {
//some android guy please help with this.
}

@ReactMethod
public void setApplicationIconBadgeNumber(int badgeNumber) {
//some android guy please help with this.
}
}
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ export default {
return DeviceEventEmitter.addListener(eventName, payload => callback(payload));
}
},
getApplicationIconBadgeNumber: (callback) => {
if (Platform.OS === 'ios') {
return RNPusherPushNotifications.getApplicationIconBadgeNumber(callback);
} else {
return RNPusherPushNotifications.getApplicationIconBadgeNumber(callback);
}
},
setApplicationIconBadgeNumber: (badgeNumber) => {
if (Platform.OS === 'ios') {
RNPusherPushNotifications.setApplicationIconBadgeNumber(badgeNumber);
} else {
RNPusherPushNotifications.setApplicationIconBadgeNumber(badgeNumber);
}
}
};
13 changes: 13 additions & 0 deletions ios/RNPusherPushNotifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ - (dispatch_queue_t)methodQueue
});
}

RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback) {
dispatch_async(dispatch_get_main_queue(), ^{
NSInteger badgeCount = [[UIApplication sharedApplication] applicationIconBadgeNumber];
callback([NSNumber numberWithInteger:badgeCount]);
});
}

RCT_EXPORT_METHOD(setApplicationIconBadgeNumber:(nonnull NSNumber*)badgeNumber) {
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].applicationIconBadgeNumber = [badgeNumber integerValue];
});
}

- (void)handleNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [UIApplication sharedApplication].applicationState;
Expand Down