-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Notification not showing up in foreground after triggering displayNotification() (Android) #988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It seems as if the requirements for displaying a foreground notification are stricter on Android. I was stuck on this same issue (notifications showing only in the background) and adding Here is my configuration: import React, { Component } from 'react';
import firebase from 'react-native-firebase';
import { Platform } from 'react-native';
class App extends Component {
componentDidMount() {
const channel = new firebase.notifications.Android.Channel(
'channelId',
'Channel Name',
firebase.notifications.Android.Importance.Max
).setDescription('A natural description of the channel');
firebase.notifications().android.createChannel(channel);
// the listener returns a function you can use to unsubscribe
this.unsubscribeFromNotificationListener = firebase.notifications().onNotification((notification) => {
if (Platform.OS === 'android') {
const localNotification = new firebase.notifications.Notification({
sound: 'default',
show_in_foreground: true,
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.android.setChannelId('channelId') // e.g. the id you chose above
.android.setSmallIcon('ic_stat_notification') // create this icon in Android Studio
.android.setColor('#000000') // you can set a color here
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
} else if (Platform.OS === 'ios') {
const localNotification = new firebase.notifications.Notification()
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.ios.setBadge(notification.ios.badge);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
}
});
// ...
}
componentWillUnmount() {
// this is where you unsubscribe
this.unsubscribeFromNotificationListener();
}
// ...
}; |
Yes, Android controls whether these notifications are shown in the foreground based on the fields that you've defined. Be aware that this may differ across different versions of Android so it's worth consulting the official documentation to find out what you need to include: https://developer.android.com/guide/topics/ui/notifiers/notifications.html |
@mitchellbutler you're solution seems that it should be working on first go. but I am still not been able to show notifications when app is foreground. it is showing in console though. |
@yashojha19, I ran into a few more issues after posting the config above. I've just updated it now with a clearer example that I can confirm is working on iOS and Android. In combination with the official docs it should be enough to get things working. |
@mitchellbutler can I ask what is the purpose of this code
suppose I want to send notification via firebase console for testing do I still need that code to be declared ? Thank you in advance. |
@mitchellbutler , Hey there and thanks for helping new developers, I am trying out your code for remote push notifications . Foreground works just fine I added a console.log to verify it. But when I am on the background and I tap on the notification my app starts and nothing happens,no Console,log like expected.I am aware of the chanelId required by Android. I set the chanel Id on firebase console the same string as the one I declared on your hardcoded setup.Any ideas how i can fix this? |
@tsavlis, it sounds like you have notifications displaying properly and you want to do something when you tap one. In that case you need to read the documentation on This is unrelated to the issue mentioned above and I don't want to pollute the issues for this project. If you read through the documentation and those functions are not behaving properly then I recommend searching through existing issues and opening a new one if none of them are relevant. |
I have alrdy made a new topic
https://stackoverflow.com/questions/51061999/react-native-firebase-starter-kit-push-notifcactions-not-working
I just searched for solution at my problem and found your thread. Still
cant figure out what I am doing wrong , I have alrdy read and copied the
instructions you linked me
2018-06-27 7:25 GMT-09:00 Mitchell Butler <[email protected]>:
… @tsavlis <https://github.com/tsavlis>, it sounds like you have
notifications displaying properly and you want to do something when you tap
one. In that case you need to read the documentation on
onNotificationOpened or getInitialNotification here
<https://rnfirebase.io/docs/v4.2.x/notifications/receiving-notifications#4>
-Listen-for-a-Notification-being-opened).
This is unrelated to the issue mentioned above and I don't want to pollute
the issues for this project. If you read through the documentation and
those functions are not behaving properly then I recommend searching
through existing issues and opening a new one if none of them are relevant.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#988 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ATdcqgrYOEPg1jLJ3g5zaxbeFHHpk-2Lks5uA7HugaJpZM4TYAq2>
.
|
Sorry I haven't come back here earlier but in my case adding a
|
@mitchellbutler i do this but get error could not send notifications ? :\ |
@mitchellbutler Hi, i did try your snippet but apparently, it is giving me an error const channel = new firebase.notifications.Android.Channel(
EDIT so, apparently, you need a logo (required). otherwise your notification won't render |
@mitchellbutler can you share your complete implementation? I can't get the notifications to show in the foreground. Thanks! |
I disabled the sound from Firebase Cloud Mesagging, then it works! Thanks @mitchellbutler |
@mitchellbutler work like a charm. You saved my day. Thanks |
I changed |
How to do this set up in v7? I confuse in new docs now |
Issue
When the app is in foreground, I'd like to show possible remote notifications.
So I use
new firebase.notifications.Notification()
to create a new notification when a remote notification has been received and then triggerfirebase.notifications().displayNotification();
to display it as it says in the docs.It works well on iOS but not on Android (doesn't even show up in the status bar).
Environment
Application Target Platform: iOS, Android
Development Operating System: macOS High Sierra
Build Tools:
React Native version: 0.55.2
RNFirebase Version: 4.0.3
Firebase Module: database, auth, messaging, notifications
The text was updated successfully, but these errors were encountered: