android - 当应用程序关闭时 fcm 通知的某些功能不起作用
问题描述
我正在使用 fcm 进行推送通知。当应用程序打开并收到通知时,通知正常工作。但是当应用程序关闭并收到通知时,某些功能(如振动、声音、添加操作等)无法正常工作。
Intent intent = new Intent(this, FullscreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String urlAddress = "https://cafebazaar.ir/";
Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress));
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent2.setPackage("com.farsitel.bazaar");
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.macanads)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setColor(Integer.parseInt(color))
.setSound(soundUri)
.setVibrate(new long[] { 1000, 1000, 1000})
.addAction(android.R.drawable.ic_menu_directions,"بروزرسانی",pendingIntent2)
.setContentIntent(pendingIntent).build();
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder);
}
解决方案
我的问题已经解决了。您不能将 JSON 密钥“通知”放在对 Firebase API 的请求中,而应使用“数据”
在以下情况下使 firebase 库调用您的 onMessageReceived()
前台应用
后台应用
应用程序已被杀死
您不得将 JSON 密钥“通知”放在对 Firebase API 的请求中,而是使用“数据”,见下文。
当您的应用程序处于后台或被杀死时,以下消息将不会调用您的 onMessageReceived(),并且您无法自定义通知。
推荐阅读
- django - 检索具有特定参与用户的所有聊天
- regex - 来自网络爬虫函数的这个正则表达式背后的逻辑是什么: ("^(http|www)((?!("+linkParse+")).)*$")
- git - 合并时 GIt 保持当前版本(我们的版本或他们的版本)
- unity3d - 如何手动安装对统一的 android 构建支持(统一集线器)?
- python - 如何在打印的表格中添加行号
- php - 通过元键排除 Wordpress 循环中的帖子
- wordpress - 如何将基于主题位置的 WordPress 菜单项作为简单的字符串/文本?
- angular - 将对象作为属性传递给指令
- powershell - PowerShell:DynamicParam:获取传递参数的列表?
- regex - 小十进制数的正则表达式匹配在实体框架中失败