zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.77k stars 2.05k forks source link

How to show background notifications with in app using react-native andorid #677

Closed shaikhussian closed 4 years ago

shaikhussian commented 6 years ago

I need to show notification content with in app. For foreground I am able to show notification content with in app. But when app is in background I can't. When user tap on notification in background, i want to show that notification content with in app. Can any one help me to fix this?

Thanks in advance!

hedshafran commented 6 years ago

https://github.com/zo0r/react-native-push-notification#handling-notifications

aspidvip commented 6 years ago

this is not work in Android

aspidvip commented 6 years ago

but worked is iOS

hedshafran commented 6 years ago

You are half right :) This doesn't work on Android when you're running a debug variant. But it DOES work if you run a release variant. Just tested it on Android, the 'onNotification' callback is being called with the notification data when you click the notification while the app is in the background/dead...
Just run $ react-native run-android --variant=release to see for yourself.

(Obviously you will not be able to debug it.. So show a toast from the callback or something)

aspidvip commented 6 years ago

release variant is not work on Android

hedshafran commented 6 years ago

Look dude, I'm trying to help you here.. But you only say stuff don't work and 'thumb-down' me, without posting any errors or code samples... So good luck solving your issues.

ljuborados commented 6 years ago

So I'm trying to set this up, and as far as I can tell, the onNotification callback is not called when notification is received, only when it's tapped on (and that works on debug and release).

adirzoari commented 6 years ago

@hedshafran @ljuborados I'm also facing error when I get notification only I'm on forgreound and not in background without any error. how can I fix it?

ljuborados commented 6 years ago

@adirzoari honestly I haven't found a fix, but I only needed this for scheduled local notifications, so I'm just saving each notification to the store, and filtering them depending on current time, it's a really hacky solution but at least it works.. I am using OneSignal for push notifications, and with OneSignal I haven't had a single issue, even though they collect (a lot) of user data - so that might be a big minus depending on your use case..

adirzoari commented 6 years ago

Do you have example that run with onesignal?

ljuborados commented 6 years ago

@adirzoari I was using react-native-onesignal, there is an example in the repo

brunogonncalves commented 5 years ago

I have tested here, but it's really don't working in Background. I executed the app using react-native run-android --variant=release

I wrote a simple code:

const notification = new firebase.notifications.Notification();
const date = new Date();
 date.setSeconds(date.getSeconds() + 5);
    notification
      .setSound("default")
      .setNotificationId('quiz')
      .setTitle(`Olá, `)
      .setBody('Nos ajude respondendo algumas perguntas.')
      .setData({
        action: 'openOut',
        value: 'https://www.winsocial.com.br'
      });

    if (Platform.OS === 'android') {
      notification.android
        .setChannelId('winsocial')
        .android.setPriority(firebase.notifications.Android.Priority.Max);
    } 
    // date.setMinutes(date.getMinutes() + 1);

    firebase
      .notifications()
      .scheduleNotification(notification, {
        fireDate: date.getTime()
      })
      .catch(err => alert(err.message));
samaneh-kamalian commented 4 years ago

i have same issue anybody can solve it ?

pavm035 commented 4 years ago

Same issue

andifirwansyah commented 4 years ago

I have same issue @shaikhussian, but i solve with adding default_notification_channel_id in the AndroidManifest.xml

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />

and res/values/strings.xml <string name="default_notification_channel_id">YOUR_CHANNEL_ID</string>

https://github.com/invertase/react-native-firebase/issues/2215#issuecomment-518687184

AftabUfaq commented 4 years ago

I am able to get the notification when my app is running means foreground. but when the app is background it didn't receive any notification. I am using firebase so when data is added to firebase then it well triggers that function. my code is: class CustomerMainScreen extends Component { constructor(props){ super(props); this.state = { uid:'', mechaniclocation:'', problemlocation:'', problemid:'', timeanddate: new Date().toLocaleString(), } this.notif = new NotifService(); } componentDidMount(){ firebase.database().ref('Problems').on('child_added', snapshot => { this.notif.localNotif(); } } }