![](/img/trans.png)
[英]Is it possible to start an application when a background notification is received in a locked android or ios?
[英]how to start background job when a notification received
大家好,這是我的第一個問題,希望能得到答復。 我有一個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,也遇到了同樣的問題。
收到通知時,有什么方法可以觸發后台任務?
不尋常的贊賞,一個好的答案:))
我不確定這項工作是否可行,但是您可以嘗試。 創建一個類並擴展Service
然后在內部創建AsyncTask
,例如:
public class TestServices extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask.execute();
return START_NOT_STICKY;
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
// preform your task here, downloading saving etc.
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent(TestServices.this, TestServices.class);
stopService(intent);
}
}
}
不要忘記在清單中注冊您的Service
類。
然后在方法onMessageReceived
內部進行一些更改。 嘗試這個:
Intent notificationIntent = new Intent(mContext, TestServices.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0);
希望可能對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.