我正在构建一个能够从FirebaseMessagingService捕获意图的服务。
我使用的第一个版本在主要活动的应用程序级别执行,但我不想再在此级别处理它。我创建了一个intent服务,如下所示:
package com.seb.sebastien.appservices;
import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;
import android.util.Log;
public class appsControlIntentService extends IntentService {
public static final String REQUEST_MSG = "request";
public static final String RESPONSE_MSG = "response";
public static final String TAG = "appsControlIntentService";
private String type_sim;
public appsControlIntentService() {
super("appsControlIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
boolean request = intent.getBooleanExtra(REQUEST_MSG, false);
if(request == true) {
sendResponse("Enable");
handleAction(getCarrier());
registerAllReceivers();
}
else if (request == false) {
sendResponse("Disable");
unRegisterAllReceivers();
disableAction();
}
else
Log.e(TAG, "Error msg");
}
...
/* Firebase and SIM */
private BroadcastReceiver serviceBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast received");
if (intent.getExtras().getString(appFirebaseMessagingService.FIREBASE).equals(appFirebaseMessagingService.FIREBASE))
type_sim = intent.getExtras().getString(appFirebaseMessagingService.SIM_TYPE);
else if(intent.getExtras().getString(SimNotificationService.EXTRA_SIM_STATE) != null)
Log.d(TAG, "Get something from Sim");
else
type_sim = null;
if(type_sim != null){
handleAction(type_sim);
}
}
};
private void registerAllReceivers(){
Log.d(TAG, "Broadcast receiver register");
registerReceiver(serviceBroadcastReceiver, new IntentFilter(appFirebaseMessagingService.INTENT_FILTER));
}
private void unRegisterAllReceivers() {
if (serviceBroadcastReceiver != null) {
unregisterReceiver(serviceBroadcastReceiver);
serviceBroadcastReceiver = null;
}
}
@Override
public void onDestroy() {
unRegisterAllReceivers();
super.onDestroy();
}
}
此意图服务注册一个广播接收器,以便能够接收如下所示的firebase广播:
package com.seb.sebastien.appservices;
public class appFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "appFirebaseMessagingService";
public static final String INTENT_FILTER = "INTENT_FILTER";
public static final String FIREBASE = "firebase";
public static final String SIM_TYPE = "simtype";
public appFirebaseMessagingService() {
}
/* @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
} */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String sim = null;
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sim = remoteMessage.getNotification().getBody();
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
Intent intent = new Intent(INTENT_FILTER);
intent.putExtra(FIREBASE, FIREBASE);
intent.putExtra(SIM_TYPE, sim );
sendBroadcast(intent);
}
}
调试时,我看到我们一直走到sendBroadcast,但似乎没有收到" appControlIntentService"如果我已注册接收器,则为类事件。
知道为什么没有在intentservice中收到广播?
答案 0 :(得分:2)
首先,不确定您实际创建appsControlIntentService
的实例的位置。
其次,当onHandleIntent
完成时,IntentService会停止。它意味着做一个简短的任务并完成。所以基本上即使你创建了一个来收听广播,它也只能在非常有限的时间内完成,直到调用onDestroy
为止。
关于用于收听自定义意图的BroadcastReceiver解决方案:
1)创建一个在后台运行的服务(不是IntentService,常规服务),并让它注册一个BroadcastListener。您可以在活动首次唤醒时创建此服务,也可以在Boot BroadcastReceiver中创建。确定您是希望它是前台服务还是后台服务。由于内存问题,后台服务更有可能被操作系统杀死。但前台服务要求您在状态栏中显示一个图标。
2)只需将BroadcastReceiver放入清单中。这样,操作系统甚至会唤醒应用程序以在广播到达时处理广播。即使应用程序当前没有运行。 看看" Manifest声明的接收器" in:https://developer.android.com/guide/components/broadcasts.html。
要禁用此接收器,请查看:Android - how to unregister a receiver created in the manifest?。或者,您可以在共享首选项中保存是否要让它对事件执行某些操作,或者更好的是,如果首先禁用FirebaseMessagingService,则不要从FirebaseMessagingService发送意图。
答案 1 :(得分:0)
你应该像这样使用闹钟管理器作为广播接收器 -
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(MainActivity.this, BroadCastReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,
alarmIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ duration(in millisecond), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ in millisecond, pendingIntent);
}