I'm developing the notification feature using react-native-firebase
on React native
in Android
.
Most of the features(such as remote notification on foreground and background, displaying local notification) are well implemented, but there's one problem I haven't solved.
As I said above, receiving notifications on foreground and background are working well. When device's screen is off(lock mode or doze mode) the notification is well received with vibrating and sound but screen is not on. I have tested on Emulator(Nexus 5X with Google play), LG G Flex2, Samsung galaxy S8.
Expectation
Wake up device (turn on screen) when notifications arrived on Android
Current Situation
When notifications arrived, sounds and vibrations are working, but screen is off.
I haven't tested on iOS. Here are my configurations.
gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
build.gradle (project)
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.1'
}
build.gradle (app)
compileSdkVersion 26
minSdkVersion 16
targetSdkVersion 26
React Native Version
"react": "^16.3.0-alpha.1",
"react-native": "0.54.3",
react-native-firebase version
"react-native-firebase": "^4.2.0",
client-side receiving notification codes
async configureNotifications() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.listenNotification();
} else {
try {
await firebase.messaging().requestPermission();
this.listenNotification();
} catch (e) {
Alert.alert('', '알림 거부하시면 화재 감지 알림을 받으 실 수 없습니다.');
}
}
}
listenNotification() {
if (Platform.OS === 'android') {
const channel = new firebase.notifications.Android.Channel(
'alert',
'alert',
firebase.notifications.Android.Importance.Max,
).setDescription('My apps alert');
firebase.notifications().android.createChannel(channel);
}
const subProm = [];
const { account } = this.props;
const { deviceInfo } = account;
for (let i = 0; i < deviceInfo.length; i++) {
subProm.push(firebase.messaging().subscribeToTopic(deviceInfo[i].deviceID));
}
Promise.all(subProm).then(() => {
this.messageListner = firebase.messaging().onMessage((message) => {
let newNotification;
if (Platform.OS === 'android') {
newNotification = new firebase.notifications.Notification()
.setNotificationId(message.messageId)
.setTitle(message.data.title)
.setBody(message.data.body)
.setSound('default')
.android.setPriority(firebase.notifications.Android.Priority.High)
.android.setChannelId('alert');
}
return firebase.notifications().displayNotification(newNotification);
});
});
}
client-side background Headless Task handler
export default async (message) => {
console.log('on Background Message');
console.log(message);
let newNotification;
if (Platform.OS === 'android') {
newNotification = new firebase.notifications.Notification()
.setNotificationId(message.messageId)
.setTitle(message.data.title)
.setBody(message.data.body)
.setSound('default')
.android.setPriority(firebase.notifications.Android.Priority.High)
.android.setChannelId('alert');
}
return firebase.notifications().displayNotification(newNotification);
};
index.js
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
server-side sending notification codes (test version)
nodejs with firebase-admin
var msg = {
android: {
ttl: 36000,
data: {
title: 'title',
body: 'body' + topic,
},
priority: 'high',
},
topic,
};
admin
.messaging()
.send(msg)
.then((res) => {
console.log('Successfully sent message', res);
process.exit();
}, console.error);