react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

how to set notificaton badge number when app background/closed in ios

Open nguyenvanphuc2203 opened this issue 8 years ago • 6 comments

it work perfectly when app foreground and not work when app background and closed. my code:

onNotification: (notification) => {
                Device.detectNotification({"data_extra onNotification DEBUG DATA ":notification});
                if(Platform.OS === "ios")
                {
                    if(notification.message.notification_id){
                        if ( notification.foreground ){
                            // alertDropDown IOS 
                            ios_noti_data = notification.message;
                            this.dropdown.alertWithType('custom', notification.message.title, notification.message.body);
                        }
                    }
                    
                }
                // set badge number icon app
                let notification_redux = store.getState();
                if (  Platform.OS === 'ios' ){
                    PushNotificationIOS.setApplicationIconBadgeNumber(notification_redux.notification_badge+1)
                }else{
                    RNBadgerAndroid.setBadge(notification_redux.notification_badge+1)
                }
                // count number natification unread to notification badge
                setTimeout(()=> { 
                    NotificationAPI.getNotificationUnRead()
                },1000)

                // process the notification
                // Handle When User Tap on notification forceground Android
                const clicked = notification.userInteraction;
                if (clicked ) {
                    Device.detectNotification({"data_extra userInteraction ":notification});
                    let nofify_data;
                    if (Platform.OS === "ios"){
                        nofify_data = notification.message;
                    }else{
                        nofify_data = notification;   
                    }
                    _handleNavigateScreenForSubUrl(nofify_data);
                }
                notification.finish(PushNotificationIOS.FetchResult.NoData);
            },

nguyenvanphuc2203 avatar Sep 13 '18 02:09 nguyenvanphuc2203

I'm having the same issue, does anyone have an idea on this ?

MarkRamsden avatar Sep 27 '18 14:09 MarkRamsden

Ok not obvious but eventually get to the bottom of it. look here https://github.com/zo0r/react-native-push-notification/blob/master/trouble-shooting.md you need to send silent or mixed notifications

MarkRamsden avatar Sep 28 '18 13:09 MarkRamsden

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar May 02 '20 00:05 github-actions[bot]

Any updates on it?

alexfov avatar Jul 23 '21 13:07 alexfov

+1

yannickpschroeder avatar Jan 19 '22 12:01 yannickpschroeder

  1. You need @react-native-firebase/messaging
  2. use content_available: true when you send push from server
  3. Catch messages with https://rnfirebase.io/messaging/usage#background--quit-state-messages
  4. Write your badge update logic using PushNotificationIOS.setApplicationIconBadgeNumber()

Finally I managed to solve the problem/

alexfov avatar Jan 19 '22 17:01 alexfov