当app在前台时,如何在前面而不是在带有通知图标的状态栏上显示通知?

时间:2017-04-11 16:32:41

标签: android firebase firebase-cloud-messaging

我的代码如下所示:

private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, OrderHistory.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Consts.NOTIFICATION_ORDER_ID, notificationMap.get(Consts.NOTIFICATION_ORDER_ID));
        intent.putExtra(Consts.NOTIFICATION_ORDER_STATUS,
                notificationMap.get(Consts.NOTIFICATION_ORDER_STATUS));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("QuFlip")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0, notificationBuilder.build());
    }

现在,每当通知到来时,它会在状态栏中显示android bydefault app图标。但我想要做的是,我想显示正确的通知,就像应用程序没有运行时一样。

2 个答案:

答案 0 :(得分:1)

尝试setLargeIcon(R.mipmap.ic_launcher)。 如果它没有帮助,请尝试添加:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notificationBuilder.setPriority(Notification.PRIORITY_HIGH); }

答案 1 :(得分:1)

设置PRIORITY_HIGH和DEFAULT_SOUND标志:

        notification.setPriority(Notification.PRIORITY_HIGH);
        notification.setDefaults(NotificationCompat.DEFAULT_SOUND);

如果您设置了DEFAULT_VIBRATE,它也可以工作,但是更具侵入性。