【问题标题】:Android - Notification received but no notification popup (Foreground)Android - 收到通知但没有弹出通知(前台)
【发布时间】:2019-02-20 13:09:51
【问题描述】:

我的应用正在正确接收通知,但未能显示带有接收信息的通知弹出窗口(如果应用已打开)。

注意:如果应用程序在后台,则显示通知不会有任何问题。


我的代码:

我通过这种方式收到通知:

@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 吗?
  • @RahulArora Firebase
  • 可以发布从通知中收到的有效载荷吗?
  • 还需要onMessageReceived 代码。由于onMessageReceived 中的某些情况,很可能sendNotification 没有被调用。

标签: android firebase notifications google-cloud-messaging


【解决方案1】:

如果您使用 FCM,此图片说明了一切。

【讨论】:

  • 谢谢,你的图表很有启发性。但是,我在前台使用了这两个有效负载,并且我将我的通知发送到系统托盘。
  • @SoliQuiD 是的,但是您也会在 onMessageReceived 中接到电话,然后由您决定如何处理。
【解决方案2】:

好吧,在我的情况下,我传递到消息正文中的一些数据在 onReceived 中为空。

修复了这个问题,我的通知弹出窗口再次显示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多