我可以在FirebaseMessagingService中启动前例任务吗?

问题描述 投票:0回答:1

我想通过接收Firebase的通知来开始一个较长的过程(例如上传文件)。我不想启动新的前台服务,但想在同一类中处理上传,如下所示:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification() != null) {
        final String title=remoteMessage.getNotification().getTitle();
        final String tag = remoteMessage.getNotification().getTag();
        String body = remoteMessage.getNotification().getBody();

        if (tag.equals("start")) {
            startUpload(title,body);
        }
    }
}


public void startUplaod(String title,String body){
    ShowNotification(title,body);
    // and start upload
}

public void ShowNotification(String title,String text){
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(text)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .build();
    startForeground(1, notification);
}
}

当我向正在运行的设备发送通知时,有时我在服务器上收到目标文件,有时却没有!通知不固定,我不确定上传任务是否已完成?我猜想完成任务有时间限制(可能是几秒钟),在此之后,升级失败。

在[[FirebaseMessagingService类中启动长任务是否有任何限制?换句话说,startForeground是否可以强制FirebaseMessagingService长时间启动前台任务?

android firebase-cloud-messaging foreground-service
1个回答
0
投票
    对于提供了onMessageReceived的所有消息,您的服务应在收到后20秒内处理任何消息(在Android Marshmallow中为10秒)。时间窗口可能会更短,具体取决于调用onMessageReceived之前发生的操作系统延迟。之后,各种操作系统行为(例如Android O的后台执行限制)可能会干扰您完成工作的能力。
  • 您可以使用JobIntentService上载任务吗,收到通知后我会制作一个示例下载图像。

    class ImageDownloadPushService : JobIntentService() { override fun onHandleWork(intent: Intent) { val bitmap = downloadImage("url") Log.e("ImageDownload", "$bitmap") } private fun downloadImage(address: String?): Bitmap? { // Convert string to URL val url = getUrlFromString(address) // Get input stream val inputStream = url?.let { getInputStream(it) } ?: return null // Decode bitmap // Return bitmap result return decodeBitmap(inputStream) } private fun getUrlFromString(address: String?): URL? { return try { URL(address) } catch (e1: Throwable) { null } } private fun getInputStream(url: URL): InputStream? { var inputStream: InputStream? // Open connection val conn: URLConnection try { conn = url.openConnection() conn.connect() inputStream = conn.getInputStream() } catch (e: IOException) { inputStream = null } return inputStream } private fun decodeBitmap(inputStream: InputStream): Bitmap? { var bitmap: Bitmap? try { // Turn response into Bitmap bitmap = BitmapFactory.decodeStream(inputStream) // Close the input stream inputStream.close() } catch (e: IOException) { bitmap = null } return bitmap } companion object { private const val JOB_ID = 101 fun enqueueWork(context: Context, work: Intent) { enqueueWork(context, ImageDownloadPushService::class.java, JOB_ID, work) } }

    }
  • 在onMessageReceived()

    override fun onMessageReceived(context: Context, remoteMessage: ToastRemoteMessage) { ImageDownloadPushService.enqueueWork(context, Intent()) }

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.