发送fcm且应用程序处于打开状态时Android应用程序崩溃

时间:2017-05-16 17:06:07

标签: android firebase service firebase-cloud-messaging

我正在使用firebase和我的Android应用程序发送推送通知。当我发送消息并且应用程序关闭时,会收到通知,意图有效并且一切正常。但是当应用程序打开并发送消息时,它会因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++;
    }
}

知道可能导致这种情况的原因是什么?感谢。

2 个答案:

答案 0 :(得分:2)

试试这个

.setColor(ContextCompat.getColor(this,R.color.colorAccent));

答案 1 :(得分:0)

该行:

.setColor(getColor(R.color.colorAccent))

导致你的问题。由于您在FirebaseMessagingService类中调用它,因此Android正在寻找那里的方法。您可能必须引用活动上下文才能使用它。