Android可以在5.0系统以上显示横幅通知,代码如下:

private void showNotification() {
    NotificationManager notifyManager = (NotificationManager) mBleService.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mBleService);
    String appName = mBleService.getString(R.string.app_name);
    builder.setContentTitle(appName)
        .setSmallIcon(R.drawable.app_icon)
        .setContentText("通知内容");

    PendingIntent pIntent = PendingIntent.getActivity(mBleService, 1, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pIntent);
    builder.setAutoCancel(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      builder.setVisibility(Notification.VISIBILITY_PUBLIC);
      // 关联PendingIntent
      builder.setFullScreenIntent(pIntent, false);
    }
    Notification notification = builder.build();

    notifyManager.notify(NOTIFICATION_ID, notification);

}

然后在用户设置-通知管理中开启悬浮通知允许
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐