【发布时间】:2019-12-16 10:24:17
【问题描述】:
我想通过接收来自 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长时间启动前台任务吗?
【问题讨论】:
-
你是怎么解决的?
-
没有办法。完成任务有时间限制。 @user60108
标签: android firebase-cloud-messaging foreground-service