【问题标题】:How to show notifiction top screen in android?如何在android中显示通知顶部屏幕?
【发布时间】:2020-01-09 19:30:28
【问题描述】:

我编写此代码以在 android 中显示通知。但此通知仅显示在状态栏中。我希望收到消息时在顶部屏幕上显示通知,例如电报应用:

enter image description here

我的代码:

        NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        String offerChannelId = "offerChannelId";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String offerChannelName = "offerChannelName";
            String offerChannelDescription = "offerChannelDescription";
            int offerChannelImportance = NotificationManager.IMPORTANCE_HIGH;
            @SuppressLint("WrongConstant") NotificationChannel notifChannel = new NotificationChannel(offerChannelId, offerChannelName, offerChannelImportance);
            notifChannel.setDescription(offerChannelDescription);
            mNotifyManager.createNotificationChannel(notifChannel);
        }

        NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
        sNotifBuilder.setSmallIcon(R.drawable.ic_notifications)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notifications))
                .setColor(getResources().getColor(R.color.baseColor_yellow))
                .setContentTitle("title")
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(Notification.PRIORITY_MAX);
        mNotifyManager.notify(1, sNotifBuilder.build());

我是怎么做到的? 最小 SDK 为 21。 提前致谢。

【问题讨论】:

标签: android


【解决方案1】:

我为您找到了解决方案。希望对您有所帮助。

//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Ping Notification")
.setContentText("Tomorrow will be your birthday.")
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
.addAction(R.drawable.dismiss, getString(R.string.dismiss), piDismiss)
.addAction(R.drawable.snooze, getString(R.string.snooze), piSnooze);

//set intents and pending intents to call service on click of "dismiss" action button of notification
Intent dismissIntent = new Intent(this, MyService.class);
dismissIntent.setAction(ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

//set intents and pending intents to call service on click of "snooze" action button of notification
Intent snoozeIntent = new Intent(this, MyService.class);
snoozeIntent.setAction(ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Notification for oreo*/
            String channelId = "channel-01";
            String channelName = "Demo";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        channelId, channelName, importance);
                notificationManager.createNotificationChannel(mChannel);
            }
//to post your notification to the notification bar with a id. If a notification with same id already exists, it will get replaced with updated information.
notificationManager.notify(0, builder.build());

最低 SDK 是 Lolipop。

【讨论】:

  • 接受我的道歉。我无法传达我的意思。我希望收到消息时在顶部屏幕上显示通知,例如电报。 (添加到帖子以了解我的意思的图片)
  • 这就是所谓的抬头通知。搜索创建提醒通知。
  • 您也可以查看此 SO 帖子。 stackoverflow.com/questions/33510861/…
猜你喜欢
  • 2011-11-18
  • 2014-11-30
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
相关资源
最近更新 更多