大家好,这是我在这里的第一个问题,所以我希望得到答案。我有一个 android 应用程序,在 FCM 的帮助下,我能够在前台、后台和应用程序被终止时接收通知。
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
final String jobTag="copyDb";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
我想在通知到达时开始后台作业,我使用了 firebase jobDispatcher:
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("fromUser", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("fromUser",from_user_id);
editor.commit();
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
Job myJob = dispatcher.newJobBuilder()
.setService(jobDispatcher.class) // the JobService that will be called
.setTag(jobTag)
.build();
dispatcher.mustSchedule(myJob);
但是当应用程序在后台或被杀死时它不起作用,但我收到通知。
我试图将后台任务移动到 FCM onMessageReceived 并且也遇到了同样的问题。
收到通知时有什么方法可以触发后台任务吗?
对一个好的答案的不寻常的赞赏:))