我正在使用Firebase和我的安卓应用程序发送推送通知。当我发送消息并且应用程序关闭时,会收到通知,意图有效并且一切都很好。但是当应用程序打开并且我发送消息时,它会崩溃,并显示 getColor()
方法中的此错误:
Exception java.lang.NoSuchMethodError: No virtual method getColor(I)I in class Lcom/kinectafrica/android/service/FirebaseMessagingService; or its super classes (declaration of 'com.kinectafrica.android.service.FirebaseMessagingService' appears in /data/app/com.kinectafrica.android-1/base.apk)
com.kinectafrica.android.service.FirebaseMessagingService.sendNotification (FirebaseMessagingService.java:45)
com.kinectafrica.android.service.FirebaseMessagingService.onMessageReceived (FirebaseMessagingService.java:33)
com.google.firebase.messaging.FirebaseMessagingService.zzo ()
com.google.firebase.messaging.FirebaseMessagingService.zzn ()
com.google.firebase.messaging.FirebaseMessagingService.zzm ()
com.google.firebase.iid.zzb$2.run ()
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
java.lang.Thread.run (Thread.java:818)
这是我FirebaseMessagingService
课:
package com.kinectafrica.android.service;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
import com.kinectafrica.android.R;
import com.kinectafrica.android.activity.SplashScreenActivity;
/**
* Made by acefalobi on 5/16/2017.
*/
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
int notifyId = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("Service", "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d("Service", "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d("Service", "Message notification: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getData().get("message"));
}
private void sendNotification(String message) {
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.putExtra("isNotify", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notify)
.setColor(getColor(R.color.colorAccent))
.setContentTitle("Kinect")
.setWhen(System.currentTimeMillis())
.setContentText(message)
.setAutoCancel(true)
.setSound(uri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notifyId, builder.build());
notifyId++;
}
}
知道是什么原因造成的吗?谢谢。
试试这个
.setColor(ContextCompat.getColor(this, R.color.colorAccent((;
行:
.setColor(getColor(R.color.colorAccent))
导致您的问题。 由于您是在FirebaseMessagingService类中调用它,因此Android正在寻找该方法。 您可能需要引用活动上下文才能使用它。