我使用Firebase Cloud Messaging发送推送通知.

这是我的FirebaseMessageService:

public class FireBaseMessageService extends FirebaseMessagingService {

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.e("TAG", "From: " + remoteMessage.getFrom());

Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+" : "+remoteMessage.getData().get("CardCode"));

sendNotification(remoteMessage.getNotification().getBody());

}

private void sendNotification(String messageBody) {

Intent intent = new Intent(this, StartActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher_final)

.setContentTitle("Notification")

.setContentText(messageBody)

.setTicker("Test")

.setAutoCancel(true)

.setDefaults(Notification.DEFAULT_SOUND)

.setContentIntent(pendingIntent);

NotificationManager notificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

}

}

和FirebaseInstanceServer:

public class FirebaseInstanceService extends FirebaseInstanceIdService {

@Override

public void onTokenRefresh() {

// Get updated InstanceID token.

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Log.e("TAG", "Refreshed token: " + refreshedToken);

// TODO: Implement this method to send any registration to your app's servers.

sendRegistrationToServer(refreshedToken);

}

private void sendRegistrationToServer(String token) {

// Add custom implementation, as needed.

Log.e("TAG", "Refreshed token2: " + token);

}

}

哪个在AndroidManifest中声明:

android:name=".util.notifications.FireBaseMessageService">

android:name=".util.notifications.FirebaseInstanceService">

所以问题是,当应用程序运行的代码显示得很好,通知有默认声音,但是当应用程序在前台或没有运行通知没有任何声音和代码未显示在状态栏.

为什么会发生这种情况,我该如何解决?

android 程序运行不了,当应用程序在后台运行或不运行时,Android – 推送通知工作不正确...相关推荐

  1. firebase 推送_如何使用Firebase向Web应用程序添加推送通知?

    firebase 推送 by Leonardo Cardoso 由莱昂纳多·卡多佐(Leonardo Cardoso) 如何使用Firebase向Web应用程序添加推送通知? (How to add ...

  2. 微信小程序开发工具 将已存在的Git项目在新开发者下拉取推送

    微信小程序开发工具 将已存在的Git项目在新开发者下拉取推送 前言 流程 完事 前言 最近有同事离职了,交接了一个微信小程序的项目下来,源码放在了公司内部的GIT上面,当我下载下来维护之后发现,在微信 ...

  3. 小程序前端消息推送通知

    小程序前端消息推送通知(采用的是HTTPS调用) 应用场景: 用户在支付订单后想要获取支付成功的及金额变动的一个消息通知 1. 小程序后台找到订阅消息和制作我们需要的模版并获取相应的模版ID 2.需要 ...

  4. linux 后台运行jar包命令,Linux 运行jar包命令(Cent OS 7后台运行jar包)

    Linux 运行jar包命令如下: 方式一 特点:当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 那如何让窗口不锁定? 方式二 java -jar shareniu. ...

  5. android自定义push通知_20个海外Web和App推送通知服务工具

    在App和网站中使用推送通知有不同的原因,并且在提高流量和与客户互动方面有很多好处.推送通知是一种交互式可点击消息,可将访问者直接引导至你的网站.它们可以帮助你以指数方式增加流量和参与率.因此,营销人 ...

  6. Android推送通知指南(转)

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  7. Android 推送通知指南(转载)

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  8. Android推送通知指南

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  9. java推送数据给安卓,java – 如何从Firebase推送通知中获取数据并将其显示在Android Activity中?...

    对不起这个noob问题,我是 android开发的新手.我目前正在开发一个项目,需要向安装了我的应用程序的 Android设备发送推送通知.我已经按照firebase的快速入门教程完成了这项工作,并在 ...

最新文章

  1. python操作Mysql基础
  2. 如何在SQL Management Studio中指定其他端口号?
  3. Golang 入门系列(十) mysql数据库的使用
  4. (47)逆向分析 KiSystemService 函数填充 _KTRAP_FRAME 部分
  5. 克隆需要验证_GeneCopoeia基因克隆
  6. git pull时冲突的几种解决方式
  7. 16张图带你吃透高性能 Redis 集群
  8. 44.分治算法练习:  一元三次方程求解
  9. yum安装软件出错解决方法
  10. 九章算法--寻找数组波峰
  11. 启动凤凰系统出现android,凤凰系统重启后出现蓝屏时的解决方法
  12. 七人表决器VHDL代码
  13. 数字密码锁设计(利用数字逻辑电路的电子密码锁)
  14. Matlab中求解总体标准差和样本标准差的区别(std函数)
  15. Javase MINA框架
  16. 微信公共号申请图文详解
  17. Element UI 中国省市区级联数据js
  18. 龙卷风路径_龙卷风的目录
  19. matlab 贪吃的蛇,贪吃的蛇教案
  20. HTML如何使用隐藏图片,css3如何隐藏图片?

热门文章

  1. 用神经网络测量训练集的半衰期
  2. 做一个可以和时空分类的神经网络
  3. 用反向传导进行分子动力学模拟并比较NN二甲基苯胺,N甲基苯胺,苯胺,硝基苯的定位效应
  4. 3.12 总结-深度学习第五课《序列模型》-Stanford吴恩达教授
  5. 一起学nRF51xx 15 - spis
  6. 【工具】Excel 表格数据转换成Json格式的实用工具 excel2json
  7. linux 进程防火墙连接管理,Linux系统管理初步(五)系统防火墙控制程序firewalld...
  8. C#8.0可空引用类型的使用注意要点
  9. python List,切片的用法
  10. 用CSS伪元素制作箭头