Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PushNotificationIOS.setApplicationIconBadgeNumber() doesn't work if the app is in background state. #5

Open
3 tasks done
cpojer opened this issue Feb 15, 2019 · 6 comments

Comments

@cpojer
Copy link
Contributor

cpojer commented Feb 15, 2019


This issue was originally created by @JulienUsson as facebook/react-native#22740.


Environment

 React Native Environment Info:
    System:
      OS: macOS 10.14.2
      CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
      Memory: 40.18 MB / 8.00 GB
      Shell: 5.6.2 - /usr/local/bin/zsh
    Binaries:
      Node: 11.0.0 - ~/.nvm/versions/node/v11.0.0/bin/node
      Yarn: 1.12.1 - ~/.nvm/versions/node/v11.0.0/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v11.0.0/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23, 24, 25, 26, 27
        Build Tools: 23.0.1, 24.0.3, 25.0.2, 26.0.1, 26.0.2, 27.0.3
    IDEs:
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.6.3 => 16.6.3 
      react-native: 0.57.8 => 0.57.8 
    npmGlobalPackages:
      react-native-cli: 2.0.1

Description

PushNotificationIOS.setApplicationIconBadgeNumber() doesn't work on IOS if the app is in background state.

Reproducible Demo

import React, {Component} from 'react'
import {PushNotificationIOS, Text, View} from 'react-native'
import OneSignal from 'react-native-onesignal'

export default class App extends Component {
    state = {
        notification: null
    }

    componentDidMount() {
        OneSignal.init("token", {kOSSettingsKeyAutoPrompt: true})
        OneSignal.inFocusDisplaying(0)
        OneSignal.addEventListener('received', this.onReceived)
    }

    componentWillUnmount() {
        OneSignal.removeEventListener('received', this.onReceived)
    }

    onReceived = async notification => {
        PushNotificationIOS.setApplicationIconBadgeNumber(1)  // doesn't work in background
        this.setState({notification}) // work in background
    }

    render() {
        return (
            <View>
                <Text>{JSON.stringify(this.state.notification)}</Text>
            </View>
        )
    }
}
`
``
@nguyenvanphuc2203
Copy link

same issue ! . did you find solution?

@trilopin
Copy link

I found a solution for this problem.

if you are sending pushes with firebase, you can configure badge ios parameter to enable native handler.

https://firebase.google.com/docs/cloud-messaging/send-message#when_to_use_platform-specific_fields

@LuigiMaestrelli
Copy link

Anyone found a solution to this?
I have the same problem, but with getDeliveredNotifications.
I'm trying to remove a notification when the user reads the message from another source (like our web app)

@pwn-0x309
Copy link

Anyone found a solution to this issue? I have the same problem too! :(

@dominiczaq
Copy link

Correct me if I'm wrong but I believe iOS restricts all apps in background.

    onReceived = async notification => {
        PushNotificationIOS.setApplicationIconBadgeNumber(1)  // doesn't work in background
        this.setState({notification}) // work in background
    }

I think onReceived fires when your app transitions back to active state so it 'looks' like setState worked.

@FtyTw
Copy link

FtyTw commented Feb 6, 2020

For oneSignal background handling of quantity of badges on iOS try out do not use onesignal listener but PushNotificationIOS.addEventListener('notification',callback), works fine for me, and ofc don't forget that your oneSignal headings should contain "content_available":true field within for wake up the app on push receiving

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

7 participants