0

現在、特にサイレントプッシュ通知で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()); 
    } 
} 
+0

あなたが間違いなくアプリケーションが実行されていないときにコンテキストを取得することができます。あなたの通知を受け取った場所のクラス全体を表示します。 GCMを使用している場合は、メッセージを受信した関数がある必要があります。コンテキストにはパラメータ –

+0

が必要です。@TomerShemeshは、投稿の最後にGCM通知を受け取るクラスを追加しました。それは私がする必要があることを意味する: 'mWebServiceCoordinator = new WebServiceCoordinator(CONTEXT_OF_MYGCMLISTENER_SERVICE、this);'? – Eliott

+0

下記の私の答えを見てください。私はあなたの問題を解決するかもしれないと思う。 –

答えて

0

[OK]をイムあなたはGCMサービス内部コンテキストを

mWebServiceCoordinator = new WebServiceCoordinator(getApplicationContext(), this); 
+0

私はsendPingNotification()の最初の行を修正していると確信しています。明日は試してみます(今は仕事ではありません)。私はあなたに知らせるでしょう – Eliott

+0

私はあなたがシングルトンに文脈を得る方法を変えるべきだと思います。あなたが持っている方法は良い方法ではありません。あなたはそれがあなたのサービスでそれを作成する場合、それがメインアクティビティコンテキストを使用しようとしないように格納されるコンテキストでシングルトンをinstansiateする必要があります –

0

だから私は取得するgetApplicationContextを使用することができますかなり確信して以下のように私sendPingNotification()方法を編集した:

private void sendPingNotification() { 
    mWebServiceCoordinator = new WebServiceCoordinator(getApplicationContext(), this); 
    UserSingleton.setContext(getApplicationContext()); 
    mWebServiceCoordinator.fetchSessionConnectionData(UserSingleton.getInstance().getLogin(), 
      UserSingleton.getInstance().getPassword(), 
      UserSingleton.getInstance().getDeviceId(), 
      UserSingleton.getInstance().getDeviceType(), 
      UserSingleton.getInstance().getAvailability()); 
} 

私はにsetContext()方法を追加しました、私はgetApplicationContext()として自分のUserSingletonのコンテキストを設定することができます。コンテキストはもうnullではなく、getContext()は有効なContextを返します。すべてが機能しています!あなたに2つありがとう。

関連する問題