我正在使用Firebase,并在应用程序处于后台时测试从我的服务器向我的应用发送通知.通知成功发送,甚至出现在设备的通知中心,但是当通知出现或甚至我点击它时,我的FCMessagingService内的onMessageReceived方法永远不会被调用.
当我在我的应用程序位于前台时对此进行测试时,调用了onMessageReceived方法,一切正常.当应用程序在后台运行时会出现此问题.
这是预期的行为,还是有办法解决这个问题?
这是我的FBMessagingService:
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FBMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i("PVL", "MESSAGE RECEIVED!!");
if (remoteMessage.getNotification().getBody() != null) {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getNotification().getBody());
} else {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getData().get("message"));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我按照Firebase指南添加FCM,因此我将以下依赖项添加到我的应用程序gradle:
compile 'com.google.android.gms:play-services:9.0.0'
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)
这个项目对我的项目有所帮助:
classpath 'com.google.gms:google-services:3.0.0'
Run Code Online (Sandbox Code Playgroud)
在此之后,我通过下载并将其添加到我的app目录,从Firebase控制台设置添加了google-services.json.
现在我收到这个错误:
错误:.dex文件中的方法引用数不能超过64K.请访问https://developer.android.com/tools/building/multidex.html,了解如何解决此问题
如果我按照说明操作并让我的应用程序支持multidex,我的应用程序会在启动后立即崩溃.
将multidex添加到我的应用程序后出现以下错误:
05-20 01:25:32.253 19812-19812/com.cryogenos.pearsonvisionlimousine W/dalvikvm: VFY: unable to resolve static field 8723 (common_google_play_services_unknown_issue) in Lcom/google/android/gms/R$string;05-20 01:25:32.253 19812-19812/com.cryogenos.pearsonvisionlimousine W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416b5e30)05-20 01:25:32.253 19812-19812/com.cryogenos.pearsonvisionlimousine E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.cryogenos.pearsonvisionlimousine, PID: 19812java.lang.NoClassDefFoundError: com.google.android.gms.R$stringat com.google.android.gms.common.internal.zzah.<init>
(Unknown Source)
at com.google.firebase.FirebaseOptions.fromResource(Unknown Source)
at com.google.firebase.FirebaseApp.zzbu(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1609)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1574)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5643)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5206)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5143)
at android.app.ActivityThread.access$1500(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
at …Run Code Online (Sandbox Code Playgroud) 我的服务中有一个广播接收器,当我从我的活动发送广播以停止服务时,我收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference
Run Code Online (Sandbox Code Playgroud)
这是我的服务类:
public class PhraseService extends Service{
public static List<Phrase> phrases;
private Context context;
private static final String PHRASE_SERVICE_BROADCAST_ID = "com.cryogenos.safephrase.PHRASE_SERVICE_BROADCAST_ID";
private BroadcastReceiver command_broadcast_receiver;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.context = getApplicationContext();
LocalBroadcastManager.getInstance(this).registerReceiver(command_broadcast_receiver, new IntentFilter(PHRASE_SERVICE_BROADCAST_ID));
Log.i("COS", "STARTED SELF");
/* Handle any command sent by the app here */
command_broadcast_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent …Run Code Online (Sandbox Code Playgroud)