Hyu*_*Lee 7 android react-native react-native-firebase
我正在使用react-native-firebase
on React native
in开发通知功能Android
。
大部分功能(如前台后台远程通知、显示本地通知)都实现的很好,但有一个问题我没有解决。
正如我上面所说,在前台和后台接收通知运行良好。当设备的屏幕关闭(锁定模式或打瞌睡模式)时,通过振动和声音很好地接收通知,但屏幕不亮。我已经在模拟器(带有 Google play 的 Nexus 5X)、LG G Flex2、三星 Galaxy S8 上进行了测试。
当通知到达 Android 时唤醒设备(打开屏幕)
收到通知时,声音和振动正常,但屏幕关闭。
我还没有在 iOS 上测试过。这是我的配置。
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Run Code Online (Sandbox Code Playgroud)
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.1'
}
Run Code Online (Sandbox Code Playgroud)
compileSdkVersion 26
minSdkVersion 16
targetSdkVersion 26
Run Code Online (Sandbox Code Playgroud)
"react": "^16.3.0-alpha.1",
"react-native": "0.54.3",
Run Code Online (Sandbox Code Playgroud)
"react-native-firebase": "^4.2.0",
Run Code Online (Sandbox Code Playgroud)
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);
});
});
}
Run Code Online (Sandbox Code Playgroud)
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);
};
Run Code Online (Sandbox Code Playgroud)
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1760 次 |
最近记录: |