Jam*_*mes 12 java notifications android
仅在 android 11 和 10 中出现错误,其他都很好,已经检查过这个其他问题,我的代码似乎没问题,有什么想法吗?
startForeground 失败并出现 Bad notification for startForeground
android.app.RemoteServiceException:startForeground 的错误通知
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String message = intent.getStringExtra("message");
int id = intent.getIntExtra("ID", 1);
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Intent firstActivityIntent = new Intent(this, FirstActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(FirstActivity.class);
stackBuilder.addNextIntent(firstActivityIntent);
Intent notificationUpdateIntent = new Intent(this, UpdateNotificationActivity.class);
notificationUpdateIntent.putExtra("ID", id);
stackBuilder.addNextIntent(notificationUpdateIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setContentTitle("Reminder")
.setContentText(message)
.setSmallIcon(R.drawable.noteappicon)
.setStyle(new NotificationCompat.BigTextStyle())
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
Run Code Online (Sandbox Code Playgroud)
我在 Android 11 中遇到错误
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2005)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Run Code Online (Sandbox Code Playgroud)
这是 10 中的错误
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel1 pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x50 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 10
我对频道 ID 的错误需要创建一些频道 ID,就像我过去的问题一样
我为 Channel id 创建了另一个类
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is channel 1");
NotificationChannel channel2 = new NotificationChannel(
CHANNEL_2_ID,
"Channel 2",
NotificationManager.IMPORTANCE_LOW
);
channel2.setDescription("This is channel 2");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14202 次 |
| 最近记录: |