微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android-React Native:处理无声推送通知

我使用react-native-firebase为我们的React Native应用(用于android和iOS)处理推送通知.

我注意到,在应用程序运行(前台后台)时收到的推送通知只有1个回调,而在关闭或终止时则没有.

firebase
.notifications()
.onNotification(notification => {
    console.log('Notification received');
);

但是,如果该应用程序关闭或被杀死,它只会将通知放在托盘中,而不会执行上面的console.log.

然后输入静推送通知.因此,当我仅在通知的有效负载中发送数据部分时,即使应用程序位于前台,也不会触发上面的回调.

我看不到其他有助于接收静推送通知的回调.

那么我们如何处理javascript部分中的推送通知

最佳答案
您不需要其他答案中建议的其他软件包.
使用RNFirebase.io,您可以轻松处理此问题.

如果您在应用程序处于后台的情况下收到通知,则必须自己处理它才能显示通知.例如,请参见我的用于推送通知的init-Method.

  import firebase from 'react-native-firebase';
  const notifications = firebase.notifications();
  ....
  notifications.onNotification((notif) => {
    notif.android.setChannelId('app-infos');
    notifications.displayNotification(notif);
  });

您可以通过displayNotification来实现.但是请确保在调用之前设置了Notification-Channel,因为否则它将无法在> = Android 8.0上运行

顺便说一句:请确保您已完全设置Firebase并授予所有必需的权限,以便在应用关闭或在后台运行时能够监听通知. (https://rnfirebase.io/docs/v5.x.x/notifications/android)

附录

我以这个为例来说明如何将firebase-notification-stuff实现为一个小型库(如果不需要,请删除redux-stuff):

import firebase from 'react-native-firebase';
import { saveNotificationToken } from 'app/actions/firebase';
import reduxStore from './reduxStore';
import NavigationService from './NavigationService';

const messaging = firebase.messaging();
const notifications = firebase.notifications();
const crashlytics = firebase.crashlytics();

function registerNotifChannels() {
  try {
    // Notification-Channels is a must-have for Android >= 8
    const channel = new firebase.notifications.Android.Channel(
      'app-infos','App Infos',firebase.notifications.Android.Importance.Max,).setDescription('General information');

    notifications.android.createChannel(channel);
  } catch (error) {
    crashlytics.log(`Error while creating notification-channel \n ${error}`);
  }
}

// This is the Promise object that we use to initialise the push
// notifications. It will resolve when the token was successfully retrieved. The
// token is returned as the value of the Promise.
const initPushNotifs = new Promise(async (resolve,reject) => {
  try {
    const isPermitted = await messaging.hasPermission();

    if (isPermitted) {
      registerNotifChannels();

      try {
        const token = await messaging.getToken();
        if (token) {
          resolve(token);
        }
      } catch (error) {
        crashlytics.log(`Error: Failed to get notification-token \n ${error}`);
      }
    }
  } catch (error) {
    crashlytics.log(`Error while checking notification-permission\n ${error}`);
  }

  // If we get this far then there was no token available (or something went
  // wrong trying to get it)
  reject();
});

function init() {
  // Initialise the push notifications,then save the token when/if it's available
  initPushNotifs.then(token => reduxStore.dispatch(saveNotificationToken(token)));

  // Save the (new) token whenever it changes
  messaging.onTokenRefresh(token => reduxStore.dispatch(saveNotificationToken(token)));

  notifications.onNotification((notif) => {
    notif.android.setChannelId('app-infos');
    notifications.displayNotification(notif);
  });

  notifications.onNotificationopened((notif) => {
    const { notification: { _data: { chatroom: chatRoomId } } = {} } = notif;

    if (chatRoomId) {
      NavigationService.navigate('ChatRoom',{ chatRoomId });
    }
  });
}

export default {
  init,};

这样,只需转到index.js文件(或应用程序的根文件,以及如何命名),然后调用init-Metod即可:

...
import LPFirebase from 'lib/LPFirebase';

LPFirebase.init();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐