当应用程序在后台或被杀死时,图像未显示在推送通知中?

时间:2019-07-24 14:12:08

标签: react-native react-native-firebase

我正在用React native开发一个应用程序,想要在应用程序处于后台或被终止时在推送通知中显示Image。当应用程序在前景中运行良好时,但在后台仅显示标题和消息,但不显示图像。对于推送通知,我正在使用 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"
}

我对本机反应非常陌生,感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您的有效载荷包含一个notification键,则只有当您的应用程序处于活动状态/处于前台(将其传递到onMessageReceived处理程序时),应用程序才会处理推送消息。否则(因此它在后台,或者被杀死了),FCM会使用您放入通知密钥有效负载中的值来为您显示通知。

如果您的有效载荷仅包含一个data键,则您的应用程序将自行处理所有推送消息。它们都已交付给您的onMessageReceived处理程序。

请参阅此参考资料:https://stackoverflow.com/a/37876727/3037523