当我收到通知时,我的应用程序崩溃了,这是我处理通知的代码:
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());
}
}
NotificationHandler.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:一天前工作正常。感谢。
答案 0 :(得分:2)
只是为了让你知道,我在几个小时后发现了问题,
你可以在我的gradle文件中看到,我添加了这个:编译'com.android.support:recyclerview-v7'
当我应该添加的是这个:
编译'com.android.support:recyclerview-v7:21.0。+'
gradle自动添加了24.0的版本。+因为我没有指定版本(21.0。+)
这导致了问题,我的项目库中有错误的库。
我希望它能帮助其他有同样问题的人。
感谢所有人的帮助!
答案 1 :(得分: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);
}
答案 2 :(得分:0)
检查此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
}
答案 3 :(得分:-1)
针对此类问题的解决方案是将整个项目迁移到应解决所有问题的Android x。 that's is the documentation to migrate them
如果您将应用程序编译为sdk版本号27,则有必要迁移到android x,因为第二个平面中的通知无法正常工作