java.lang.NoSuchMethodError: Landroid/support/v4/app/通知兼容$生成器类中没有直接方法V;或其超类

rsl1atfo  于 2022-12-28  发布在  Android
关注(0)|答案(2)|浏览(167)

我被推送通知卡住了。我正在处理旧版本代码。我已经更新了它,并在应用程序中添加了firebase依赖项build.gradle。当应用程序在后台运行时,它会崩溃,并显示以下错误。但在前台,我收到了一条通知。请帮助我。build grade在这里

E/AndroidRuntime: FATAL EXCEPTION: Firebase-MyFirebaseMessagingService
Process: PID: 9735
java.lang.NoSuchMethodError: No direct method <init>(Landroid/content/Context;Ljava/lang/String;)V in class Landroid/support/v4/app/NotificationCompat$Builder; or its super classes (declaration of 'android.support.v4.app.NotificationCompat$Builder' appears in /data/app/com.affichi-2/split_lib_dependencies_apk.apk)
    at com.google.firebase.messaging.zzb.zzf(Unknown Source)
    at com.google.firebase.messaging.zzc.zzas(Unknown Source)
    at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source)
    at com.google.firebase.iid.zzb.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:818)

请检查此类:-

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    try {
        if (remoteMessage.getNotification() != null) {

            Log.e("remoteMessage1", "=" + remoteMessage.getNotification().getTitle());
            Log.e("remoteMessage2", "=" + remoteMessage.getNotification().getBody());
            Log.e("remoteMessage5", "=" + remoteMessage.getData());
            sendNotification(remoteMessage.getData());
            //sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getData());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void sendNotification(String title, String messageBody, java.util.Map<java.lang.String, java.lang.String> getData) {
    try {
        Intent intent = null;

        intent = new Intent(this, HomeActivity.class);

        int oneTimeID = (int) SystemClock.uptimeMillis();
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, oneTimeID /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification.Builder notificationBuilder =
                new Notification.Builder(this)
                        .setSmallIcon(R.drawable.app_icon)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setSound(defaultSoundUri)
                        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Afifchi",
                    NotificationManager.IMPORTANCE_DEFAULT);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(channel);
        }
        assert notificationManager != null;

        notificationManager.notify(oneTimeID /* ID of notification */, notificationBuilder.build());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void sendNotification(Map<String, String> messageBody) {
    Intent intent = new Intent(this, MainActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(messageBody.get("title"))
                    .setContentText(messageBody.get("message"))
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0, notificationBuilder.build());
}

}

frebpwbc

frebpwbc1#

您正在使用NotificationCompat.Builder (Context context)的已弃用构造函数
请改用此构造函数:NotificationCompat.Builder (Context context, String channelId)

s5a0g9ez

s5a0g9ez2#

用这个代替。

Notification notification =
            new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle(getText(R.string.notification_title))
                    .setContentText("Waiting for internet connection to forward sold products while offline")
                    .setSmallIcon(R.drawable.ic_notification_orange)
                    .setContentIntent(pendingIntent)
                    .setTicker("Hello Benjie")
                    .build();

相关问题