Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit c43e446

Browse files
Merge pull request #1235 from gogoout/feature/auto-clear-badge
feat(message) add flag to disable the auto clearing badge
2 parents 5d5130f + d3e0f8e commit c43e446

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

docs/MESSAGING.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ curl -X POST --header "Authorization: key=SERVER_KEY" --Header "Content-Type: ap
175175

176176
<img src="images/push-server-key.png" width="459px" height="220px" alt="Push server key"/>
177177

178-
> Note that if you don't want a badge on the app icon, remove the `badge` property or set it to 0. Note that launching the app clears the badge anyway.
178+
> Note that if you don't want a badge on the app icon, remove the `badge` property or set it to 0.
179179
180180
### Notfication-popup example
181181

@@ -303,6 +303,15 @@ Add the `showWhenInForeground` flag to your payload:
303303
}
304304
```
305305

306+
### (iOS) Disable automatic clearing the badge
307+
Currently by default, when launching the app, badge is cleared. You can disable the feature by passing a flag in your init option.
308+
309+
```js
310+
firebase.init({
311+
autoClearBadge: false
312+
});
313+
```
314+
306315
## What if iOS doesn't show/receive notifications in the background?
307316
Make sure you [`require` the plugin in `app.ts` / `main.ts` / `main.aot.ts`](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/55cfb4f69cf8939f9101712fed22383196b08d36/demo/app/app.ts#L5)
308317
*before* `application.start()`, and do `init()` *after* the app has started (not in `app.ts` - not even in a timeout; move it out of `app.ts` entirely!).

src/firebase.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ export interface MessagingOptions {
107107
* Currently used on iOS only. Default false.
108108
*/
109109
showNotificationsWhenInForeground?: boolean;
110+
111+
/**
112+
* Automatically clear the badges on starting.
113+
* Currently used on iOS only. Default true.
114+
*/
115+
autoClearBadge?:boolean;
110116
}
111117

112118
/**

src/messaging/messaging.ios.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ let _messagingConnected: boolean = null;
2323
let _firebaseRemoteMessageDelegate: FIRMessagingDelegateImpl;
2424
let _showNotifications: boolean = true;
2525
let _showNotificationsWhenInForeground: boolean = false;
26+
let _autoClearBadge: boolean = true;
2627

2728
// Track whether or not registration for remote notifications was request.
2829
// This way we can suppress the "Allow notifications" consent popup until the listeners are passed in.
@@ -34,6 +35,7 @@ export function initFirebaseMessaging(options) {
3435
}
3536
_showNotifications = options.showNotifications === undefined ? _showNotifications : !!options.showNotifications;
3637
_showNotificationsWhenInForeground = options.showNotificationsWhenInForeground === undefined ? _showNotificationsWhenInForeground : !!options.showNotificationsWhenInForeground;
38+
_autoClearBadge = options.autoClearBadge === undefined ? _autoClearBadge : !!options.autoClearBadge;
3739

3840
if (options.onMessageReceivedCallback !== undefined) {
3941
addOnMessageReceivedCallback(options.onMessageReceivedCallback);
@@ -516,7 +518,7 @@ function _processPendingNotifications() {
516518
}
517519
_pendingNotifications = [];
518520

519-
if (app.applicationState === UIApplicationState.Active) {
521+
if (app.applicationState === UIApplicationState.Active && _autoClearBadge) {
520522
app.applicationIconBadgeNumber = 0;
521523
}
522524
}
@@ -534,7 +536,7 @@ function _processPendingActionTakenNotifications() {
534536
}
535537
_pendingActionTakenNotifications = [];
536538

537-
if (app.applicationState === UIApplicationState.Active) {
539+
if (app.applicationState === UIApplicationState.Active && _autoClearBadge) {
538540
app.applicationIconBadgeNumber = 0;
539541
}
540542
}

0 commit comments

Comments
 (0)