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

标签 android firebase notifications google-cloud-messaging

我的应用程序可以正确接收通知,但无法显示包含收到的信息的弹出通知(如果应用程序已打开)。

注意:如果应用程序在后台,通知会毫无问题地显示。


我的代码:

我以这种方式收到通知:

@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 ,如果我为它们添加了高优先级,我可以强制通知显示为单挑通知。不幸的是,这也不起作用。

最佳答案

如果您使用的是 FCM,这张图片可以解释一切。

关于Android - 收到通知但没有弹出通知(前景),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54787141/

相关文章:

android - 在 Android 中查询 Google Play 音乐数据库

android - Eclipse 错误 "ADB server didn' t ACK,无法启动守护进程”

java - 如果我的数组达到一定长度,Android 应用程序将无法运行

iOS Swift 本地通知不是 "popping up"

Android - 如果 Activity 是第一次加载(不使用 SharedPreferences),则阻止 onResume() 函数

swift - 在 Firebase 中将 observe 转换为 observeSingleEvent 时出错

python - Firebase 身份验证 verify_id_token 错误 "super(type, obj): obj must be an instance or subtype of type"

java - 在 android studio 中设置 firebase 会出错

android - 从 Android 发送 Facebook 请求不显示消息预览并且不发送给 friend

cocoa-touch - 为什么不总是调用已注册的 UIDeviceOrientationDidChangeNotification?