Android-收到通知,但没有通知弹出窗口(前景) [英] Android - Notification received but no notification popup (Foreground)
问题描述
我的应用正确接收了通知,但未显示带有接收到的信息的弹出通知(如果已打开应用).
My app is receiving the notification correctly but is failing to show a notification pop up with the received info (If the app is opened).
N.B .:如果应用程序在后台运行,则显示的通知没有任何问题.
我的代码:
我通过这种方法收到通知:
I receive the notification in this method:
@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);
}
}
然后将其称为此消息(按照我的理解应该显示该通知):
then it calls this one (which is supposed to show the notification as i understand):
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通知
Firebase notification
{
"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"
}
}
结果
- 通知是否正确构建
- 声音正确播放
- 注意是否已放入系统托盘
- 但没有出现任何通知弹出窗口(我必须显示一个 自定义对话框)
- the notification is built correctly
- sound played correctly
- notif put in system tray
- BUT no notification popup appears (i have to show a custom dialog for that)
我的问题在于此特定行
notificationManager.notify(id, notificationBuilder.build());
无法显示通知
更新 我已经在
https://developer.android.com/guide/topics/ui /notifiers/notifications
,发现通知仅显示在通知抽屉中而不弹出(如Heads-up notifications
).
and found out that notifications only show in the notification drawer without popping up (as Heads-up notifications
).
,并根据此
如果您使用的是FCM,此图片将对所有内容进行说明.
This image explains it all if you are using FCM.
这篇关于Android-收到通知,但没有通知弹出窗口(前景)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!