收到通知时例外

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了收到通知时例外相关的知识,希望对你有一定的参考价值。

当我收到通知时,我的应用程序崩溃,这是我处理通知的代码:

Firebase消息服务:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        Context context =  getApplicationContext();
        NotificationHandler.generateNotification(context,remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
    }
}

notification handler.Java:

public static  void generateNotification(Context context,String title,String message) {

int notificationId = 001;
Intent viewIntent = new Intent(context, LoginActivity.class);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(context, 0, viewIntent, 0);

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
int myColor =
        context.getResources().getColor(R.color.white);
Notification mNotification =
        new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher1)
                //.setColor(myColor)
                .setContentTitle(title)
                .setContentText(message)
                .setLargeIcon(bitmap)
                .setContentIntent(viewPendingIntent).build();

NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(context);

notificationManager.notify(notificationId, mNotification);
}

build.gradle(App):

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        // The Fabric Gradle plugin uses an open ended version to react
        // quickly to android tooling updates
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 21
    buildToolsVersion '23.0.2'
    defaultConfig {
        applicationId "com.amit.myapp"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    //    compile 'com.android.support:appcompat-v7:24.2.0'
    //    compile 'com.android.support:design:24.2.0'
    compile files('libs/gson-2.2.2.jar')
    compile 'com.android.support:appcompat-v7:21.+'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.0-beta1'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.3@aar') {
        transitive = true;
    }
    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.firebase:firebase-messaging:9.4.0'
    //compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.firebase:firebase-ads:9.4.0'
    compile 'com.android.support:recyclerview-v7'
    compile 'com.android.support:cardview-v7:21.0.+'
}

这是我的日志。

Fatal Exception: java.lang.NoSuchMethodError: No direct method <init>(Landroid/content/Context;Landroid/app/Notification;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Landroid/widget/RemoteViews;ILandroid/app/PendingIntent;Landroid/app/PendingIntent;Landroid/graphics/Bitmap;IIZZZILjava/lang/CharSequence;ZLjava/lang/String;Ljava/util/ArrayList;Landroid/os/Bundle;IILandroid/app/Notification;Ljava/lang/String;ZLjava/lang/String;)V in class Landroid/support/v4/app/NotificationCompatApi21$Builder; or its super classes (declaration of 'android.support.v4.app.NotificationCompatApi21$Builder' appears in /data/data/com.amit.myapp/files/instant-run/dex/slice-internal_impl-24.2.0_03ea6b597af0b87f5ed6808a426ca9992cd7a314-classes.dex)
       at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:761)
       at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1561)
       at com.google.firebase.messaging.zza.zzaf(Unknown Source)
       at com.google.firebase.messaging.zza.zzas(Unknown Source)
       at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source)
       at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source)
       at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source)
       at com.google.firebase.iid.zzb$2.run(Unknown Source)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)

我究竟做错了什么 ?

P.S:前一天工作正常。

谢谢。

答案

只是为了让你知道,我在几个小时后发现了问题,

正如你在我的gradle文件中看到的,我添加了这个:

编译'com.android.support:recyclerview-v7'

当我应该添加的是这个:

编译'com.android.support:recyclerview-v7:21.0.+'

gradle自动添加了24.0的版本。+因为我没有指定版本(21.0。+)

这导致了问题,我的项目中有错误的库。

我希望它能帮助其他有同样问题的人。

谢谢大家帮忙!

另一答案

您无法在显示通知之前获取通知服务

public static  void generateNotification(Context context,String title,String message) {


    int notificationId = 001;
    Intent viewIntent = new Intent(context, LoginActivity.class);
    PendingIntent viewPendingIntent =PendingIntent.getActivity(context, 0, viewIntent, 0);

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
    int myColor =context.getResources().getColor(R.color.white);
    Notification mNotification =new android.support.v4.app.NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher1)
                    //.setColor(myColor)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setLargeIcon(bitmap)
                    .setContentIntent(viewPendingIntent).build();

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

    notificationManager.notify(notificationId, mNotification);
}
另一答案

检查这个link以了解NoSuchMethodError。

相反,你可以尝试另一种方法。在MyFirebaseMessagingService类中声明您的generateNotification方法,看看是否发生任何错误。对您的代码进行一点修改......

更换

NotificationHandler.generateNotification(context,remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

generateNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

你的generateNotification方法是:

private void generateNotification(String title, String body) {

  // build your notification here

}
另一答案

解决此类问题的方法是将整个项目迁移到Android x,以解决您的所有问题。 that's is the documentation to migrate them

如果您将应用程序编译为sdk版本号27,则必须迁移到android x,因为第二个平面中的通知不起作用

以上是关于收到通知时例外的主要内容,如果未能解决你的问题,请参考以下文章

这些片段是等价的吗? (NVL 与例外)

GCM:如果应用程序不可见,则不会收到 IntentService.sentBroadcast 的消息

在android中按下通知时如何打开片段页面

在通知单击时将项目添加到片段内的 recyclerview

如何在不与 MainActivity 交互的情况下从通知中打开片段页面?

在通知单击时启动片段而不会丢失状态