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



我的代码如下所示:

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默认应用程序图标。但我想做的是,我想显示适当的通知,就像应用程序未运行时一样。

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

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

设置PRIORITY_HIGH和DEFAULT_SOUND标志:

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

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

相关内容

  • 没有找到相关文章

最新更新