Question:
I want to automatically decrement application badge number after the push has been read in Inbox. How to perform it for React Native framework?
Answer:
First of all, please note that it's not possible to perform this feature on React Native level itself, so you'll need to make some adjustments on the Native code.
iOS:
- Open your iOS project in Xcode, go to the settings, then in header search paths add "../node_modules/pushwoosh-react-native-plugin" (recursive)
- Import "PWInbox.h" within AppDelegate.m:
#import "PWInbox.h"
3. Add the following string to applicationDidFinishLaunching:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inboxMessagesDidUpdate:) name:PWInboxMessagesDidUpdateNotification object:nil];
4. Add this method to AppDelegate.m
- (void)inboxMessagesDidUpdate:(NSNotification *)notification {
[PWInbox unreadMessagesCountWithCompletion:^(NSInteger count, NSError *error) {
[UIApplication sharedApplication].applicationIconBadgeNumber = count;
}];
}
Android:
- Open MainApplication.java and add these packages:
import com.pushwoosh.inbox.PushwooshInbox;
import com.pushwoosh.inbox.event.InboxMessagesUpdatedEvent;
import com.pushwoosh.inbox.exception.InboxMessagesException;
2. Add the following snippet to OnCreate():
EventBus.subscribe(InboxMessagesUpdatedEvent.class, new EventListener<InboxMessagesUpdatedEvent>() {
@Override
public void onReceive(InboxMessagesUpdatedEvent inboxMessagesUpdatedEvent) {
updateUnreadInboxMessages();
}
});
3. Add this method:
private void updateUnreadInboxMessages() {
PushwooshInbox.unreadMessagesCount(new Callback<Integer, InboxMessagesException>() {
@Override
public void process(@NonNull Result<Integer, InboxMessagesException> result) {
if (result.isSuccess() && result.getData() != null) {
PushwooshBadge.setBadgeNumber(result.getData());
}
}
});
}
Please note that it is also possible to use methods above within your native project.
Comments
0 comments
Please sign in to leave a comment.