当用户关闭应用程序时,如何启动/重新启动IntentService

3
我创建了一个IntentService来在您启动手机和使用应用程序时后台搜索通知。
它正在工作,但是现在我意识到当我在多任务中关闭应用程序时,我不再接收新通知。

您知道如何在用户关闭应用程序时启动(或重新启动)IntentService吗?

互联网上说这是不可能的,但是当我启动Facebook Messenger并将其关闭时,我仍然可以收到通知!

PS:我正在使用自己的Web服务器存储通知,我不想使用其他方式(如Google的解决方案),所以请节省时间,不要试图转换我:P

以下是我的代码,如果可以帮助未来的某个人或者帮助您理解:
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.antoineduval.sortirauhavre">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="fr.antoineduval.sortirauhavre._MyBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name="._MyService"
        android:exported="false"/>

</application>

BroadcastReceiver :

public class _MyBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){
        Intent startServiceIntent = new Intent(context, _MyService.class);
        context.startService(startServiceIntent);
    }
}

IntentService :

public class _MyService extends IntentService{

    private final int INTERVAL = 1000 ; // 1 sec

    private MyApplication application;
    private Timer timer;

    public _MyService(){
        super(_MyService.class.getName());
    }

    @Override
    public void onCreate(){
        super.onCreate();
        this.application = (MyApplication)getApplication();
    }

    @Override
    protected void onHandleIntent(Intent workIntent){
        this.timer = new Timer();
        timer.schedule(new TimerTask(){
            public void run(){
                try{
                    check_notification();
                } catch (IOException e){
                    e.printStackTrace();
                } catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }, 0, this.INTERVAL);
    }

    private void check_notification() throws IOException, JSONException {
        String url = MyApplication.NOTIFICATION_URL+"php/form/get.php?site="+MyApplication.NOTIFICATION_SITE_ID+"&last_notification_id="+this.application.get_cookie("last_notification_id");
        URL address = new URL(url);
        URLConnection conn = address.openConnection();
        InputStream is = conn.getInputStream();
        ArrayList<String> array = this.application.json_string_to_array(this.application.stream_to_string(is));
        for(int cpt = 0; cpt<array.size(); cpt++){
            String response = array.get(cpt);
            JSONObject obj = new JSONObject(response);

            int id = Integer.parseInt(obj.getString("id"));
            String text = obj.getString("text");
            String title = obj.getString("title");
            String action = obj.getString("url");

            if(id > Integer.parseInt(this.application.get_cookie("last_notification_id")))
                this.application.set_cookie("last_notification_id", id+"");

            this.application.send_notification(title, text, id, action);
        }
    }
}
1个回答

0
请不要混淆两件事情。Facebook有自己的SDK来管理通知服务。就像对于FCM,您只需注册一次,FCM库就会提供回调方法,您在其中接收它。所以坦率地说,Facebook通知机制并不仅仅依赖于启动和停止服务。
对于您来说,请尝试使用Service而不是IntentService,并在Service中覆盖onStart()命令,并返回布尔变量return START_STICKY。虽然这不是建议的做法,也不是完全安全的,但它将在大多数时间内工作,直到内核决定停止它。

谢谢您的帮助,为了确保我理解正确,您告诉我要将我的IntentService转换为一个Service。我尝试了一下,但是当我这样做时,我的通知不起作用了。您确定我可以使用简单的Service来创建通知吗? - Antoine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接