我做了一個IntentService在您啓動您的手機和當您在應用程序時在後臺搜索通知。
它的工作,現在我意識到,當我殺了應用程序(在多任務),我不再收到新的通知。
您知道如何在用戶終止應用程序時啓動(或重新啓動)IntentService?
關於internet它被告知這是不可能的,但當我啓動Facebook的Messenger應用程序並殺死它時,我總是會收到通知!如何啓動/重新啓動IntentService當用戶殺死應用程序
PS:我正在使用我的網絡服務器存儲通知,我不想用其他方式(如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>
廣播接收器:
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