热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无法使用react-native-firebase在Android8的前台接收通知

如何解决《无法使用react-native-firebase在Android8的前台接收通知》经验,为你挑选了1个好方法。

我无法仅在Android 8的Foreground中获得通知。也无法控制后台通知。 但是在低于8的Android版本上,可以在当前实现中正常工作。

遵循的步骤:-

    将react-native-firebase插件版本5.0.0安装到应用程序中。

    在Firebase控制台上创建了项目,并将google_service.json文件添加到android / app文件夹中。

    在AndroidManifest中添加以下代码:-

     
      
          
      
    
    
      
    


   
   
      
          
          
          
          
      
   
   
   

    在App.js页面上添加了以下函数,并在componentDidMountMethod()中调用:

performNotificationOperations(){
  this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
    console.log("Message",message);
    alert("Notification Message Arrived");
    if(this.state.isLogin){
      const notification = new firebase.notifications.Notification()
                        .setNotificationId(message.messageId)
                        .setTitle(message.data.show_name)
                        .setBody(message.data.description)
                        .setData(message.data)
                        .android.setChannelId('test_app')
                        .android.setBigPicture(message.data.showImage)
                        .android.setPriority(firebase.notifications.Android.Priority.High);
      firebase.notifications().displayNotification(notification).catch(err => alert("Error On Message"));
    }
  });
  this.notificatiOnListener= firebase.notifications().onNotification((notification: Notification) => {
    console.log("Notification=>",notification);
    alert("Notification Arrived");
    if(this.state.isLogin){
      notification.android.setChannelId('test_app')
      notification.android.setBigPicture(notification._data.showImage);
      notification.android.setPriority(firebase.notifications.Android.Priority.High)
      firebase.notifications().displayNotification(notification).catch(err => alert("Error On Notification"));
    }
  });
  this.notificatiOnOpenedListener= firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
    console.log(notificationOpen,"Opened listener");
    console.log(notificationOpen.notification._data.type,"notificationOpen");
    firebase.notifications().removeDeliveredNotification(notificationOpen.notification._notificationId)
    if(this.state.isLogin){
      if(notificationOpen.notification._data.type==='show'){
        Navigate.forward('myshowdetails', this._navigator, {show:notificationOpen.notification._data});
      }else if(notificationOpen.notification._data.type==='episode'){
        this.playEpisode(notificationOpen.notification._data.episodeToken);
        Navigate.forward('myshowdetails', this._navigator, {show:notificationOpen.notification._data});
      }
    }
  });
  firebase.notifications().getInitialNotification()
  .then((notificationOpen: NotificationOpen) => {
    if (notificationOpen) {
      alert('Initial Notification');
      console.log(notificationOpen,"notificationOpen");
      console.log(notificationOpen.notification._data.type,"notificationOpen");
      firebase.notifications().removeDeliveredNotification(notificationOpen.notification._notificationId)
      if(this.state.isLogin){
        alert('IS LOGIN TRUE');
        if(notificationOpen.notification._data.type==='show'){
          Navigate.forward('showdetails', this._navigator, {show:notificationOpen.notification._data});
        }else if(notificationOpen.notification._data.type==='episode'){
          this.playEpisode(notificationOpen.notification._data.episodeToken);
          Navigate.forward('showdetails', this._navigator, {show:notificationOpen.notification._data});
        }
      }
    }
  });
  firebase.messaging().getToken().then(token => {
    console.log("GCM Token====>>>>>>>>",token);
    Global.GCM_TOKEN=token;
    // alert(token);
    if(Global.IS_USER_LOGIN){
      Util.saveFCMToken(token);
    }
  });
}

添加了bgMessage.js文件以处理数据消息,并使用AppRegistery注册了Headless JS服务。

    // @flow
import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';
import type { Notification,NotificationOpen} from 'react-native-firebase';

export default async (message: RemoteMessage) => {
    // handle your message
    // console.log("Message=>",message);
    alert("Message Arrived");
    const notification = new firebase.notifications.Notification()
                      .setNotificationId(message.messageId)
                      .setTitle(message.data.show_name)
                      .setBody(message.data.description)
                      .setData(message.data)
                      .android.setChannelId('podpitara_app')
                      .android.setBigPicture(message.data.showImage)
                      .android.setPriority(firebase.notifications.Android.Priority.High);
    firebase.notifications().displayNotification(notification).catch(err => alert("Error in Background"));
    return Promise.resolve();
}

无头JS服务电话:-

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage',()=> bgMessaging);

附加信息:-

平台-Android

节点版本-v8.6.0

反应本机-0.57.0

react-native-firebase-0.5.0

面临的问题

    仅在Android 8中无法在Foreground中接收通知。

    在背景/最小化状态的情况下,希望以大图显示通知,但不想从我可以处理显示通知的地方获得控制权。

    在调试模式下,应用程序会正常显示图像,并在通知托盘中显示图像,而在释放模式下,应用程序不会显示图像。

请让我知道,我做错了。



1> Pierre..:

得到它的工作。

这是因为从Android 8开始,您需要创建一个频道

// Build a channel
const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
  .setDescription('My apps test channel');

// Create the channel
firebase.notifications().android.createChannel(channel);

https://rnfirebase.io/docs/v4.2.x/notifications/android-channels

我只是在用

android.setChannelId(message.data.channelId)

现在,当应用程序处于前台时,它会显示出来。

我的全部功能是这样的

const newNotification = new firebase.notifications.Notification()
            .android.setChannelId(message.data.channelId)
            .setNotificationId(message.messageId)
            .setTitle(message.data.title)
            .setBody(message.data.body)
            .setSound("default")
            .setData(message.Data)
            .android.setAutoCancel(true)
            .android.setSmallIcon('ic_notification')
            .android.setCategory(firebase.notifications.Android.Category.Alarm)

    // Build a channel
    const channelId = new firebase.notifications.Android.Channel(message.data.channelId, channelName, firebase.notifications.Android.Importance.Max);

    // Create the channel
    firebase.notifications().android.createChannel(channelId);
    firebase.notifications().displayNotification(newNotification)

希望能帮到你


推荐阅读
  • 基于移动平台的会展导游系统APP设计与实现的技术介绍与需求分析
    本文介绍了基于移动平台的会展导游系统APP的设计与实现过程。首先,对会展经济和移动互联网的概念进行了简要介绍,并阐述了将会展引入移动互联网的意义。接着,对基础技术进行了介绍,包括百度云开发环境、安卓系统和近场通讯技术。然后,进行了用户需求分析和系统需求分析,并提出了系统界面运行流畅和第三方授权等需求。最后,对系统的概要设计进行了详细阐述,包括系统前端设计和交互与原型设计。本文对基于移动平台的会展导游系统APP的设计与实现提供了技术支持和需求分析。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 使用正则表达式爬取36Kr网站首页新闻的操作步骤和代码示例
    本文介绍了使用正则表达式来爬取36Kr网站首页所有新闻的操作步骤和代码示例。通过访问网站、查找关键词、编写代码等步骤,可以获取到网站首页的新闻数据。代码示例使用Python编写,并使用正则表达式来提取所需的数据。详细的操作步骤和代码示例可以参考本文内容。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
  • Jquery 跨域问题
    为什么80%的码农都做不了架构师?JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念。当然 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
author-avatar
KNN
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有