我可以在 FirebaseMessagingService 中启动预告任务吗?



我想通过接收来自 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长时间启动前台任务吗?

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

    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)
    }
    }
    

    }

  • In onMessageReceived((

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

相关内容

  • 没有找到相关文章

最新更新