Ringo
Ringo

Reputation: 1293

Any method for accumulating the badge number with Firebase?

This question maybe duplicate,but I want to ask it again, cause perhaps some guy know how to solve it.

As we know, we can set the badge before push a notification with Firebase.If the application is in background and a remote notification is received,the badge number 1 will shown on the App icon.

Then we set the badge value to 3 on the firebase and push the message. I want the badge number on the icon is 4 (1+3),rather than 3.In other words, if the app is in the background mode and receive 2 different messages, the badge number should be accumulated.

Is there any method to do it with Firebase? Any point is appreciated.

Upvotes: 0

Views: 4570

Answers (2)

Ahmadiah
Ahmadiah

Reputation: 476

I was like you and I could not see straight answer other than "do it on the server."

Here my way:

1) You need to set badge ( a variable you can call it whatever you want) to zero into Firebase Realtime database once you launch the app. Plus set application.applicationIconBadgeNumber = 0. Here is how I do it in AppDelegate:

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    application.applicationIconBadgeNumber = 0

    // Reset badge number to zero
    let userID = Auth.auth().currentUser?.uid

    let dbRef = Database.database().reference()
    dbRef.child("Users Info").child(userID!).updateChildValues(["badge":"0"], withCompletionBlock: { (err, ref) in
        if err != nil {
            print(err!)
            return
        }
    })

}

2) Go to index.js where a function is triggered.

...
return admin.database().ref('/Users Info/' + owner).once('value', snapshot => {

    var ownerValue = snapshot.val();

    var badgeNumber = parseInt(ownerValue.badge) + 1;

    // Notification details.
    const payload = {
      notification: {
        title: 'You have a new request!',
        body: `A new request from ${downedBy.name}.`,
        badge:`${badgeNumber}`,
        sound: 'default',
      }
    };
    ...
    ...
    // Upload the new value of badge into Firebase to keep track of the number
    return admin.database().ref('/Users Info/' + owner).update({badge:badgeNumber});
    ...
    })

Upvotes: 1

Glenn Posadas
Glenn Posadas

Reputation: 13291

You are correct, this question has been asked before several times. What you can only do with the Firebase Push Notification is to set the badge count to be displayed in the app. In other words, you cannot count or maintain the count of your badge.

You will need a server for this to store the current count of the badge in your app.

You can also use silent push notification. Apple's documentation here: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

When a silent notification is delivered to the user's device, iOS wakes up your app in the background and gives it up to 30 seconds to run. In iOS, the system delivers silent notifications by calling the application:didReceiveRemoteNotification:fetchCompletionHandler: method of your app delegate.

Upvotes: 1

Related Questions