invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.66k stars 2.21k forks source link

Increment badge number on push notification receive #1933

Closed telmen closed 5 years ago

telmen commented 5 years ago

For feature requests please visit our Feature Request Board. Is it possible to auto increment badge number for iOS when the app receives a push notification? I mean calling setBadge while in the background or hasn't launched yet?

stale[bot] commented 5 years ago

Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

pmarconi commented 5 years ago

I would also like to know this 👍

Bug-Reaper commented 5 years ago

I would also like to know if this can be done.

amanpanda commented 5 years ago

Based on some extensive research and actually contact with the firebase team itself, I've found that this is annoyingly not possible to do. It is due to restrictions imposed by Apple's Push Notification Service, so it doesn't look like this will be a feature we'll see in firebase for the foreseeable future.

At best, you can increment the app badge after the user clicks on the app icon and triggers this logic - but this, of course, is counterproductive for any use case I can think of, and I didn't do this myself.

There is simply no way around the following: you need to keep track of unseen notifications for a given user in a firebase Realtime DB/Firestore, or some other database system. Each time you want to send a notification to a user, you need to read this value, increment it, and send it as a badge using firebase messaging. From then on, each time a user enters your app, reset their counter to 0. This is unfortunately the simplest way to do this.

stale[bot] commented 5 years ago

Hello 👋, to help manage issues we automatically close stale issues. This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs. Thank you for your contributions.

DonaldN92 commented 5 years ago
if(Platform.OS=="ios"){
    PushNotificationIOS.**getApplicationIconBadgeNumber**((num)=>{ // get current number
     **numBadge**=num+1;
     PushNotification.localNotification({
       // iOS only properties
       alertAction:'view',
       // iOS and Android properties
       title:lng.lng("trackasyougo:app_name"),
       message: bigText, // (required)
       soundName: 'default',
      number: **numBadge**,               
     });
   });
}
ThienMD commented 5 years ago

how about android. I want to show badge when this app in background or closed.

mikehardy commented 5 years ago

@ThienMD well, first you'll realize that badging on Android is not officially supported in Android, so it's vendor-specific, which means it will be inconsistent no matter what (Pixel won't do it, but Motorola might, for instance, depending on launcher permissions). If you're willing to accept that, then if you do the registerHeadlessJS thing you'll be able to execute JS code in response to push notifications and badge all you want. I do this, and it's just as inconsistent as I described but that's an Android+ShortcutBadger thing, not a react-native-firebase thing. If the user completely kills the app (force close) then I think you're dead no matter what but I'm less sure on this case.

ThienMD commented 5 years ago

@ThienMD well, first you'll realize that badging on Android is not officially supported in Android, so it's vendor-specific, which means it will be inconsistent no matter what (Pixel won't do it, but Motorola might, for instance, depending on launcher permissions). If you're willing to accept that, then if you do the registerHeadlessJS thing you'll be able to execute JS code in response to push notifications and badge all you want. I do this, and it's just as inconsistent as I described but that's an Android+ShortcutBadger thing, not a react-native-firebase thing. If the user completely kills the app (force close) then I think you're dead no matter what but I'm less sure on this case.

I already add registerHeadlessJS but no thing work. Here is my code. <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); // <-- Add this line

`export default async (message) => { firebase.notifications().setBadge(5);

return Promise.resolve(message);

}`

mikehardy commented 5 years ago

no thing work

I had problems with it until I registered it very very early in my app startup. Before any render was attempted certainly

You can put console.log statements in before the registerHeadlessTask statement to see if it is even executing, you should see them if you swipe the app away on android (but not if you force close it). If not, move it closer to the first thing your app does when it starts

mikehardy commented 5 years ago

So this isn't actually possible per firebase support, but there exists workaround code above to help others, I think this is resolved

mikehardy commented 5 years ago

@dkamphuoc be specific about which comment isnunclear and I will try to elaborate if there is a clear answer. For shortcut badging on Android there is no clear answer unfortunately. If you can demonstrate shortcut badger works in a public repository, in a case where it does not work for react native firebase then there's a problem. Otherwise it's just an android difference