Android - 收到通知但没有通知弹出窗口(前景)

问题描述 投票:0回答:1

我的应用正在正确接收通知,但未能显示收到信息的通知(如果应用已打开)。

N.B。:如果应用程序在后台,则会显示通知而不会出现任何问题。


我的代码:

我收到此方法的通知:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    if(remoteMessage!=null)
    {
        String id = null;
        String title = null;
        String body = null;
        String launchPage = null;

        if(remoteMessage.getNotification()!=null)
        {
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
        }

        if(remoteMessage.getMessageId()!=null)
        {
            id = remoteMessage.getMessageId();
        }

        Log.i(TAG, "id: " + id);
        Log.i(TAG, "title: "+ title);
        Log.i(TAG, "body: " + body);

        int notif_id = 0;

        sendNotification(notif_id, title, body, launchPage);
    }
}

然后它调用这个(它应该显示我理解的通知):

private void sendNotification(int id, String title, String messageBody, String launchPage)

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(android.R.drawable.stat_sys_download)
                    .setContentTitle("OMR - "+title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
                    .setChannelId(CHANNEL_ID);

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel(notificationManager, CHANNEL_ID);
    }

    Toast.makeText(this, "Received a notif...", Toast.LENGTH_SHORT).show();

    //this line is not showing a notification
    notificationManager.notify(id, notificationBuilder.build());
}

Firebase通知

{
"data": {
    "message": "data payload only used to force using OnMessageReceived() if in BACKGROUND",
    "notif_id": 1
},
"to" : "e04OAVaRw30:APA91bGyv5_tt4IWRkurjlqkqNlCxTBV8oRne18tQ5puniHPItOMgg11kdt56t5jfZnasb4Ms-tH9xUgWQhHy2eM487llRtlM9_V_PoWJI9KSr6XgCaysiDyS",
"notification": {
    "title": "Notification",
    "body": "This is Notification 2",
    "sound":"default"
}

}


结果

  • 通知是正确构建的
  • 声音播放正确
  • 通知放入系统托盘
  • 但是没有出现通知弹出窗口(我必须显示一个自定义对话框)

我的问题在于这条特定的路线

notificationManager.notify(id, notificationBuilder.build());

没有显示通知

更新我已经阅读了有关Android通知的更多信息

https://developer.android.com/guide/topics/ui/notifiers/notifications

并发现通知只显示在通知抽屉中而不会弹出(如Heads-up notifications)。

根据这个question,如果我为他们添加了高优先级,我可以强制通知显示为单挑通知。不幸的是,这也不起作用。

android firebase notifications google-cloud-messaging
1个回答
6
投票

如果您使用FCM,此图像会解释所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.