【问题标题】:android.app.RemoteServiceException: Bad notification for startForeground when trying to send notification from a serviceandroid.app.RemoteServiceException:尝试从服务发送通知时 startForeground 的错误通知
【发布时间】:2021-06-11 12:18:58
【问题描述】:

我查看了与此错误相关的所有 SO 帖子,我可以在 Google 上找到这些帖子。其中大部分是关于在 Android 8 中添加 CHANNEL_ID 的要求。其他是由于缺少一些代码,我认为这些代码在我的系统中已修复。

我参考了this 文章,并尝试实现相同的内容。我希望发送全屏 Intent 通知。

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

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    我想,问题就在这里。 String.valueOf(R.string.channel_name) 只是给出一个包含整数 ID 的字符串。结果,由于未知/未注册的频道,您收到了错误的通知错误。因此,请使用实际的字符串。

    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);
    

    【讨论】:

    • 您是否建议使用 getString,而不是 String.valueOf?此外,我之前也尝试将相同的字符串作为值放在双引号内。那也没有用。明天早上试试这个然后回来。
    • 其实我才明白getString和String.valueOf的区别。现在更有意义了。不知道我是怎么犯这样的错误的。不过,还没有尝试。
    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2016-12-12
    • 2020-06-17
    相关资源
    最近更新 更多