前台的Firebase通知 [英] Firebase notifications in the foreground

查看:153
本文介绍了前台的Firebase通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有FireBase推送通知的问题。当我的应用程序在后台,通知即将到来,但是当我的应用程序在前台我没有收到通知,但在控制台显示通知,这意味着通知在这里,但它不显示在通知栏,你可以帮我吗?

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $公共无效onMessageReceived(RemoteMessage remoteMessage){
Log.d (OnMessage,收到);
super.onMessageReceived(remoteMessage);
Log.d(TAG,From+ remoteMessage.getFrom());
Log.d(TAG,Body+ remoteMessage.getNotification()。getBody());
String aa = remoteMessage.toString();
Intent intent = new Intent(MyFirebaseMessagingService.this,MainActivity.class);
intent.putExtra(msg,aa);
sendNotification(remoteMessage);
NotificationCompat.Builder mBuilder =
NotificationCompat.Builder(this)
.setContentText(remoteMessage.getNotification()。getBody());

// remoteMessage.getNotification()。getBody();



$ b private void sendNotification(RemoteMessage remoteMessage){

Intent intent = new Intent(this,MainActivity.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)
.setContentText(remoteMessage.getNotification()。getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 / *通知的ID * /,notificationBuilder.build());

$ / code $ / $ p
$ div class =h2_lin>解决方案

因为您在控制台中看到日志消息,所以您已经正确地处理了FCM部分,并且可能是您创建通知的问题。



如果你看在您的通知创建代码中,您缺少一些必需元素在Android上创建通知。从文档:


通知对象必须包含以下内容:


  • 由setSmallIcon()设置的小图标
  • 由setContentTitle()设置的标题

  • 详细信息文本,由所以我想如果你添加这些项目到你的通知创建,你应该看到通知区域中的通知。

    I am having problem with FireBase push notifications. When my app is in the background, notifications are coming, but when my app is in the foreground i don't receive notifications, but in console that notification is displayed, that means that notification is here but it doesn't show in notification bar . Could you help me?

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("OnMessage", "Received");
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "From " + remoteMessage.getFrom());
        Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
        String aa=remoteMessage.toString();
        Intent intent=new Intent(MyFirebaseMessagingService.this, MainActivity.class);
        intent.putExtra("msg", aa);
        sendNotification(remoteMessage);
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setContentText(remoteMessage.getNotification().getBody());
    
        // remoteMessage.getNotification().getBody();
    
    }
    
    
    private void sendNotification(RemoteMessage remoteMessage) {
    
        Intent intent = new Intent(this, MainActivity.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)
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    

    解决方案

    Like you said you are receiving the message since you are seeing the log messages in the console so you have handled the FCM part properly and the issue is likely with your creation of the notification.

    If you look at your notification creation code you are missing a couple of the required elements to create a notification on Android. From the docs:

    A Notification object must contain the following:

    • A small icon, set by setSmallIcon()
    • A title, set by setContentTitle()
    • Detail text, set by setContentText()

    So I think if you add those items to your notification creation you should see the notification in the notification area.

    这篇关于前台的Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆