現在、特にサイレントプッシュ通知でGCM通知を処理中です。現時点では、アプリが閉鎖されていても(私の通知の目的です)、私は静かな通知を受け取ります。あなたが見ることができるように、私は私のサーバーになどUserSingletonのログイン、パスワードを、...送りたいnullコンテキストでの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());
}
:問題は、私はこのサイレントプッシュ通知を受信したときに実行しようとしていますコードです。 まず、新しいWebServiceCoordinatorを作成します。このWebServiceCoordinatorは、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;
}
アプリケーションが実行されている: はここに私のUserSingletonでコンテキストを取得しようとしている機能です。しかし、アプリケーションが閉じられ、何も実行されていない場合でも、MainActivityでなくても、関数によって返されたコンテキストの値はまだNULLです。
したがって、sendPingNotification()の次の部分は機能しません... 私は、アプリケーションが実行されていないとき(論理的です)にコンテキストを取得する方法がないのは確かですが、どうすれば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);
}
アイデアはありますか?おかげ
------ ------ EDIT
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を使用している場合は、メッセージを受信した関数がある必要があります。コンテキストにはパラメータ –
が必要です。@TomerShemeshは、投稿の最後にGCM通知を受け取るクラスを追加しました。それは私がする必要があることを意味する: 'mWebServiceCoordinator = new WebServiceCoordinator(CONTEXT_OF_MYGCMLISTENER_SERVICE、this);'? – Eliott
下記の私の答えを見てください。私はあなたの問題を解決するかもしれないと思う。 –