我目前正在處理GCM通知,特別是在靜默推送通知上。目前,即使應用程序關閉(這是我通知的目標),我仍會收到無聲通知。問題是,我想接收這個無聲推送通知時要執行的代碼:使用UserSingleton與空上下文(靜默推送通知)
private void sendPingNotification() {
mWebServiceCoordinator = new WebServiceCoordinator(UserSingleton.getContext(), this);
mWebServiceCoordinator.fetchSessionConnectionData(
UserSingleton.getInstance().getLogin(),
UserSingleton.getInstance().getPassword(),
UserSingleton.getInstance().getDeviceId(),
UserSingleton.getInstance().getDeviceType(),
UserSingleton.getInstance().getAvailability());
}
正如你所看到的,我想給UserSingleton的登錄名,密碼等..發送到我的服務器。 首先,我創建一個新的WebServiceCoordinator,它在參數中使用UserSingleton的Context。這就是存在問題的地方:由於應用程序完全關閉,因此沒有任何上下文! 這裏是試圖讓語境下我UserSingleton功能:
public static Context getContext() {
if (context == null) {
if (MainActivity.isRunning) {
context = MainActivity.getContext();
} else {
Log.e(LOG_TAG, "Unable to get the context");
}
}
return context;
}
此功能可以正常使用的應用程序運行時。但是由於應用程序關閉且沒有任何內容在運行,即使不是MainActivity,函數返回的上下文的值仍然爲空
因此,sendPingNotification()的下一部分不起作用... I' m很確定沒有辦法在應用程序沒有運行的時候獲得上下文(這是合乎邏輯的),但我仍然可以如何使用我的UserSingleton方法?因爲他們需要上下文才能工作,例如getSharedValue()方法:
public String getSharedValue(String key, String defaultValue) {
final SharedPreferences data_r = UserSingleton.getContext().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
return data_r.getString(key, defaultValue);
}
任何想法?由於
------編輯------
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class MyGcmListenerService extends GcmListenerService implements WebServiceCoordinator.Listener {
private static final String TAG = "MyGcmListenerService";
private WebServiceCoordinator mWebServiceCoordinator;
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
@Override
public void onMessageReceived(String from, Bundle data) {
String type = data.getString("type");
String title = data.getString("title");
String message = data.getString("message");
Log.d(TAG, "Notification from " + from + " : (" + type + ")" + " " + message);
if (type.equals("appointment_details")) {
String nom = data.getString("nom");
String prenom = data.getString("prenom");
String mail = data.getString("mail");
String telephone = data.getString("telephone");
String state = data.getString("state");
Integer stateId = Integer.parseInt(data.getString("stateId"));
String date = data.getString("date");
String token = data.getString("token");
String length = data.getString("length");
sendDetailsRendezvousNotification(title, message, nom, prenom, mail, telephone, state, stateId, date, token, length);
}
else if (type.equals("appointment"))
sendNormalNotification(title, message);
else if (type.equals("newCall"))
sendNewCallNotification();
else if (type.equals("ping"))
sendPingNotification();
else if (type.equals("normal"))
sendNormalNotification(title, message);
}
private void sendPingNotification() {
mWebServiceCoordinator = new WebServiceCoordinator(UserSingleton.getContext(), this);
mWebServiceCoordinator.fetchSessionConnectionData(UserSingleton.getInstance().getLogin(),
UserSingleton.getInstance().getPassword(),
UserSingleton.getInstance().getDeviceId(),
UserSingleton.getInstance().getDeviceType(),
UserSingleton.getInstance().getAvailability());
}
}
即使應用程序未運行,您也可以獲取上下文。顯示收到通知的全部班級。如果你使用GCM,你應該有一個on消息接收函數,它應該有一個參數作爲參數 –
@TomerShemesh在我的文章結尾添加了接收GCM通知的類。這意味着我必須這樣做:'mWebServiceCoordinator = new WebServiceCoordinator(CONTEXT_OF_MYGCMLISTENER_SERVICE,this);'? – Eliott
請看下面的答案。我認爲這可能會解決你的問題 –