推送通知未显示在通知栏上

时间:2021-07-22 15:14:34

标签: android firebase push-notification

我已经通过关注 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());

    }
}

2 个答案:

答案 0 :(得分: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

希望能帮到你

答案 1 :(得分:0)

这就是 Firebase 推送通知的工作原理。当您的应用程序在后台时,它会显示通知。当它在前台时,只会调用 onMessageReceive,并且您必须执行一些操作(可能更改 UI)来通知用户,因为他们正在使用您的应用。