如何在小米设备上以编程方式启用抬头通知?某些应用程序(例如 Telegram)默认启用此权限。
设置截图:https : //imgur.com/a/D48G8Mz
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel( ) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setSound(createNotificationSound(),
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
return CHANNEL_ID;
}
Run Code Online (Sandbox Code Playgroud)
通知
String channelId = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelId = createNotificationChannel();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId)
.setLargeIcon(icon)
.setSmallIcon(R.mipmap.ic_mipmap_launcher)
.setContentTitle("Title")
.setTicker(getResources().getString(R.string.app_name))
.setContentText("Text")
.setSound(createNotificationSound(), AudioManager.STREAM_NOTIFICATION)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setChannelId(channelId)
.setPriority(Notification.PRIORITY_HIGH);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); …Run Code Online (Sandbox Code Playgroud) android push-notification heads-up-notifications android-push-notification xiaomi
我正在尝试启动前台服务,但收到 NoSuchMethodError。它说找不到 startForeground,但看起来它是存在的。我确实拥有清单文件中声明的服务和 android.permission.FOREGROUND_SERVICE 权限。
public void startForegroundService(Intent intent) {
Log.d(TAG, "startForegroundService: " + intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getContext().startForegroundService(intent);
} else {
getContext().startService(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
并收到此错误:
java.lang.NoSuchMethodError: No virtual method startForeground(ILandroid/app/Notification;I)V in class Lcom/volnoor/backup/core/sync/BaseForegroundService; or its super classes (declaration of 'com.volnoor.backup.core.sync.BaseForegroundService' appears in /data/app/com.volnoor.backup-aFOxNxXCGe_PmUbYxrrdHw==/base.apk!classes2.dex)
at com.volnoor.backup.core.sync.BaseForegroundService.onStartCommand(BaseForegroundService.java:27)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3567)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6687)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
Run Code Online (Sandbox Code Playgroud)
驾驶服务:
public class DriveService extends BaseForegroundService {
@Override
protected int getId() { …Run Code Online (Sandbox Code Playgroud) android android-service android-notifications android-fragments android-studio