推送通知未显示在通知栏上
Posted
技术标签:
【中文标题】推送通知未显示在通知栏上【英文标题】:Push notification is not showing on notification bar 【发布时间】:2021-09-29 21:12:44 【问题描述】:我已经通过关注 YouTube 视频实现了 firebase 推送通知,并且当应用程序在后台运行时它工作正常,但是当应用程序进入前台时它不会显示通知,但我可以在 logcat 中看到通知。
这是代码-
public class MyFirebaseMessagingService extends FirebaseMessagingService
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);
getFirebaseMessage(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
public void getFirebaseMessage(String title, String msg)
Log.d("abc", title);
Log.d("abc", msg);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "plasmatechannel")
.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
.setContentTitle(title)
.setContentText(msg)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(101 , builder.build());
【问题讨论】:
【参考方案1】:从 API 级别 26 开始,所有通知都必须分配给一个通道,以设置应用于该通道中所有通知的行为。
请尝试在您现有的代码中实现以下方式
NotificationCompat.Builder notificationBuilder = null;
try
if (Build.VERSION.SDK_INT >= 26)
try
NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel(Constant.CHANNEL_ID);
NotificationChannel channelnews = new NotificationChannel(Constant.CHANNEL_ID, "Breaking News", NotificationManager.IMPORTANCE_HIGH);
channelnews.enableLights(true);
channelnews.setShowBadge(false);
channelnews.enableVibration(true);
channelnews.setLightColor(Color.WHITE);
channelnews.setVibrationPattern(new long[]100, 200, 100, 200, 100, 200, 100);
channelnews.setSound(url,new AudioAttributes.Builder().build());
notificationBuilder = new NotificationCompat.Builder(this, Constant.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.createNotificationChannel(channelnews);
catch (Exception e)
e.printStackTrace();
else
notificationBuilder = new NotificationCompat.Builder(ctx,"")
.setSmallIcon(R.mipmap.ic_launcher);
catch (Exception e)
e.printStackTrace();
if (notificationBuilder == null)
notificationBuilder = new NotificationCompat.Builder(ctx,"")
.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSubText(subtext);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationNumber, notificationBuilder.build());
请阅读详细信息 https://developer.android.com/training/notify-user/channels
https://developer.android.com/reference/android/app/NotificationChannel
希望对你有帮助
【讨论】:
是的,问题出在频道 ID 上,我阅读了文档并找到了解决方案【参考方案2】:这就是 Firebase 推送通知的工作原理。当您的应用程序在后台时,它将显示通知。当它在前台时,只调用onMessageReceive
并且您必须执行一些操作(可能更改 UI)来通知用户,因为他们正在使用您的应用程序。
【讨论】:
我已经在“onMessageReceive”中完成了构建通知的实现,请检查我上传的代码。 您提供的代码与您在标题中提到的问题无关。请也编辑它以上是关于推送通知未显示在通知栏上的主要内容,如果未能解决你的问题,请参考以下文章