android - onMessageRecieved() 被调用,执行其代码但不显示通知
问题描述
我正在关注本教程,我正在使用 FCM 发送推送通知,但由于某种原因,代码似乎不起作用。在 onMessageRecieved() 中,我正在构建通知,使手机振动并显示通知,由于某种原因,手机振动,但没有显示通知。这是代码:
Intent intent=new Intent(this, WelcomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this
,0
,intent
,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getString(R.string.app_name));
notificationBuilder.setContentText(message);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(soundUri);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
notificationBuilder.setAutoCancel(true);
Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
如果有人能告诉我出了什么问题,那就太棒了..谢谢:)
解决方案
从您的代码和通知没有出现的事实来看,我假设您正在使用 Android 版本高于 8.0 的设备进行测试。
对于 Android 8.0 (Oreo) 及更高版本,您需要使用通知渠道,以显示通知。有关更多详细信息,请参阅此链接。
推荐阅读
- r - 如何执行线性回归而不出错?
- pine-script - 如何在多个柱满足条件后编写买入或卖出触发器,在我的情况下是高于某个 EMA 的 2 个 renko 柱
- scheme - 如何修复列表中关闭对的总和的错误
- javascript - 来自动画滑动菜单的 DIV 动画运动消失在 BODY 或 WRAPPER 后面?
- c++ - 在类的构造函数中初始化映射时如何避免内存泄漏?
- r - 使用 purrr 函数和 %>% 有效地从复杂 JSON 中提取嵌套数据帧
- android - 删除图像视图周围的空白
- machine-learning - 我是否需要同时执行网格搜索(使用交叉验证)和交叉验证方法?
- database - 数据库约束检查值(来自不同行)是否按连续顺序排列
- graphql - Shopify GraphQL 突变返回“在 \"gid\" (IDENTIFIER) 上解析错误”