【问题标题】:Bad notification for startForeground Android 11 & 10startForeground Android 11 和 10 的错误通知
【发布时间】:2021-03-20 11:23:31
【问题描述】:

仅在 android 11 和 10 中出现错误,其他的都很好,已经检查了这个其他问题,我的代码似乎没问题,伙计们

startForeground fails with Bad notification for startForeground

android.app.RemoteServiceException: Bad notification for 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);

我在 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)

这是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)

【问题讨论】:

  • android.app.RemoteServiceException:startForeground 的错误通知已添加到帖子
  • 好吧抱歉
  • 这就是我在日志中的全部内容

标签: java android notifications


【解决方案1】:

我的频道 ID 错误需要创建一些频道 ID,就像我过去的问题一样

我为 Channel id 创建了另一个类

如果 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel 通道 1 = 新 NotificationChannel( CHANNEL_1_ID, "频道 1", NotificationManager.IMPORTANCE_HIGH ); channel1.setDescription("这是频道 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);
}

}

【讨论】:

    【解决方案2】:
            PendingIntent pendingIntent = PendingIntent.GetActivity(context , startId ,intent, PendingIntentFlags.UpdateCurrent);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
               .SetContentIntent(pendingIntent)
               .SetContentTitle(title)
               .SetContentText(Message)
               .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.common_google_signin_btn_icon_dark_normal))
               .SetSmallIcon(Resource.Drawable.common_google_signin_btn_icon_dark_normal)
               .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);
    
            Notification notification = builder.Build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      相关资源
      最近更新 更多