android.app.RemoteServiceException:尝试从服务发送通知时 startForeground 的错误通知
android.app.RemoteServiceException: Bad notification for startForeground when trying to send notification from a service
我查看了与此错误相关的所有 SO 帖子,我可以在 Google 上找到这些帖子。其中大部分是关于在 Android 8 中添加 CHANNEL_ID 的要求。其他是由于缺少代码片段,我认为这些代码已在我的中修复。
我参考了 this 文章,并尝试实施相同的文章。我希望发送全屏意图通知。
Notifier.java
public class Notifier extends Service {
@Override
public void onCreate() {
super.onCreate();
Context context = this;
Intent fullScreenIntent = new Intent(context, CamView.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel doorBellChannel = new NotificationChannel(getString(R.string.channel_name), name, importance);
doorBellChannel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(doorBellChannel);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, String.valueOf(R.string.channel_name))
//.setChannelId(String.valueOf(R.string.channel_name))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setChannelId(String.valueOf(R.string.channel_name))
.setFullScreenIntent(fullScreenPendingIntent, true)
.setAutoCancel(true);
Notification incomingCallNotification = notificationBuilder.build();
Log.d(TAG, "onCreate: Here 1");
int notificationId = createID();
startForeground(notificationId, incomingCallNotification);
Log.d(TAG, "onCreate: Here 2");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public int createID(){
Date now = new Date();
return Integer.parseInt(new SimpleDateFormat("ddHHmmss", Locale.US).format(now));
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我在 AndroidManifest.xml 中有这两行:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
调用服务时,出现以下错误:
2021-06-11 17:17:27.213 9432-9432/? D/Oscillator: onCreate: Here 1
2021-06-11 17:17:27.231 9432-9432/? D/Oscillator: onCreate: Here 2
2021-06-11 17:17:27.240 9432-9432/? D/AndroidRuntime: Shutting down VM
2021-06-11 17:17:27.242 9432-9432/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.smartlock, PID: 9432
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1946)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
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:935)
2021-06-11 17:17:27.260 9432-9432/? I/Process: Sending signal. PID: 9432 SIG: 9
我想,问题就出在这里。 String.valueOf(R.string.channel_name)
只是给出一个包含整数 ID 的字符串。结果,由于 unknown/unregistered 频道,您收到了错误的通知错误。因此,使用实际的字符串。
NotificationCompat.Builder notificationBuilder = /* Do not use String.valueOf() */
new NotificationCompat.Builder(context, getString(R.string.channel_name))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setChannelId(name) /* Do not use String.valueOf() */
.setFullScreenIntent(fullScreenPendingIntent, true)
.setAutoCancel(true);
我查看了与此错误相关的所有 SO 帖子,我可以在 Google 上找到这些帖子。其中大部分是关于在 Android 8 中添加 CHANNEL_ID 的要求。其他是由于缺少代码片段,我认为这些代码已在我的中修复。
我参考了 this 文章,并尝试实施相同的文章。我希望发送全屏意图通知。
Notifier.java
public class Notifier extends Service {
@Override
public void onCreate() {
super.onCreate();
Context context = this;
Intent fullScreenIntent = new Intent(context, CamView.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel doorBellChannel = new NotificationChannel(getString(R.string.channel_name), name, importance);
doorBellChannel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(doorBellChannel);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, String.valueOf(R.string.channel_name))
//.setChannelId(String.valueOf(R.string.channel_name))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setChannelId(String.valueOf(R.string.channel_name))
.setFullScreenIntent(fullScreenPendingIntent, true)
.setAutoCancel(true);
Notification incomingCallNotification = notificationBuilder.build();
Log.d(TAG, "onCreate: Here 1");
int notificationId = createID();
startForeground(notificationId, incomingCallNotification);
Log.d(TAG, "onCreate: Here 2");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public int createID(){
Date now = new Date();
return Integer.parseInt(new SimpleDateFormat("ddHHmmss", Locale.US).format(now));
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我在 AndroidManifest.xml 中有这两行:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
调用服务时,出现以下错误:
2021-06-11 17:17:27.213 9432-9432/? D/Oscillator: onCreate: Here 1
2021-06-11 17:17:27.231 9432-9432/? D/Oscillator: onCreate: Here 2
2021-06-11 17:17:27.240 9432-9432/? D/AndroidRuntime: Shutting down VM
2021-06-11 17:17:27.242 9432-9432/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.smartlock, PID: 9432
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1946)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
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:935)
2021-06-11 17:17:27.260 9432-9432/? I/Process: Sending signal. PID: 9432 SIG: 9
我想,问题就出在这里。 String.valueOf(R.string.channel_name)
只是给出一个包含整数 ID 的字符串。结果,由于 unknown/unregistered 频道,您收到了错误的通知错误。因此,使用实际的字符串。
NotificationCompat.Builder notificationBuilder = /* Do not use String.valueOf() */
new NotificationCompat.Builder(context, getString(R.string.channel_name))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setChannelId(name) /* Do not use String.valueOf() */
.setFullScreenIntent(fullScreenPendingIntent, true)
.setAutoCancel(true);