【问题标题】:Image not showing in push notification when app is in background or killed?当应用程序处于后台或被杀死时,图像未显示在推送通知中?
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2020-02-10
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2017-04-12
      • 1970-01-01
      相关资源
      最近更新 更多