【发布时间】:2019-07-24 14:12:08
【问题描述】:
我正在使用 React Native 开发应用程序,希望在应用程序处于后台或被终止时在推送通知中显示图像。当应用程序处于前台时,它运行良好,但在后台仅显示标题和消息,但不显示图像。对于推送通知,我使用 react-native-firebase 来实现这一点。
bgMessaging.js 中的代码
import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';
export default async message => {
return Promise.resolve();
};
index.js 中的代码
import bgMessaging from './bgMessaging';
AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
bgMessaging.js 中的代码负责应用在后台时的推送
为了在应用程序处于前台时显示图像,我使用了以下代码:-
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
console.log('onNotification:');
const channel = new firebase.notifications.Android.Channel(
'fcm_default_channel',
'SAT VS ACT',
firebase.notifications.Android.Importance.Max
).setDescription('A natural description of the channel');
firebase.notifications().android.createChannel(channel);
this.unsubscribeFromNotificationListener = firebase.notifications().onNotification((notification) => {
if (Platform.OS === 'android') {
console.log('background');
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('fcm_default_channel') // 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.setBigPicture('https://picsum.photos/200/300')
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
}
});
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
console.log('onNotificationOpened:');
Alert.alert(title, body)
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
console.log('getInitialNotification:');
Alert.alert(title, body)
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
console.log("JSON.stringify:", JSON.stringify(message));
});
}
暂时使用http://pushtry.com/发送推送通知。推送的json:-
{
"to":"<FCM_TOKEN>",
"notification":
{
"title":"Working Good",
"body":"Exam vlo hbe na tor",
},
"priority":"high"
}
我对本机反应非常陌生,感谢任何形式的帮助。
【问题讨论】:
标签: react-native react-native-firebase