携帯電話の起動時とアプリの起動時にIntentServiceを使用してバックグラウンドで通知を検索しました。
私はアプリを(マルチタスクで)殺すと、私はもはや新しい通知を受け取らないことに気付きました。
ユーザーがアプリケーションを終了したときにIntentServiceをどのように起動(または再起動)できますか?
internetでは、私はFacebookのメッセンジャーアプリを起動してそれを殺すと、いつも通知を受け取ることができると言われています。ユーザーがアプリケーションを終了したときにIntentServiceを開始/再起動する方法
PS:通知を保存するためにウェブサーバーを使用していますが、私は他の方法(Googleのソリューションなど)を使用して時間を節約することはできません。P
それはFUTURで誰かを助けることができるかを理解するならば私のコードです:
の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);
}
}
}
ご協力いただきありがとうございます。ご理解いただけるように、私のintentServiceをサービスに変換するように指示されました。私は試みましたが、私がそれをしたとき、私の通知は機能しません。シンプルなサービスで通知を送信できますか? – Antoine