0

I'm trying to register two receivers, one that will receive messages from my app server through GCM and onother that will load messages from my server.

all this are in an activity called ChatActivity

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        d("Broadcast received FROM MMESSAGERECEIVER");
        Toast.makeText(context, "mMessageReceiver started", Toast.LENGTH_LONG).show();
        if(cust != null && adapter != null){
            SharedPreferences sharedPref = ChatActivity.this.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
            long userID = sharedPref.getLong(AllSystems.PREFERENCES_KEY_LOGGED_IN_USER_ID, -1);
            // Extract data included in the Intent
            String message = intent.getStringExtra("message");
            String dateCreated = intent.getStringExtra("dateCreated");
            Date d = new Date(Long.parseLong(dateCreated));
            long senderId = Long.parseLong(intent.getStringExtra("senderId"));
            Toast.makeText(context, "mMessageReceiver in the first if", Toast.LENGTH_LONG).show();
            if(senderId == userID || senderId == cust.getId()){
                Toast.makeText(context, "mMessageReceiver in the second if", Toast.LENGTH_LONG).show();
                adapter.add(new ChatMessageData(senderId == cust.getId(), message, new DateTime(d)));
                Bundle results = getResultExtras(true);
                results.putBoolean(INTERCEPTED, true);
                playSound();
            }
        }
    }
};

private BroadcastReceiver mLoadedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        d("Broadcast received");
        d("Conversation loaded broadcast received");
        if(task != null && cust != null){
            d("Contact and task not null");
            long contactId = intent.getLongExtra("contactId", -1);
            if(contactId == cust.getId()){
                d("Executing conversation loading task");
                    task.execute();
            }
        }
    }
};

private void playSound(){
    try {
        Uri notification = Uri.parse("android.resource://com.me.myapp/" + R.raw.notif);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));
    LocalBroadcastManager.getInstance(this).registerReceiver(mLoadedReceiver, loadedFilter);
}


//Must unregister onPause()
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mMessageReceiver);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mLoadedReceiver);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chats);

    LocalBroadcastManager.getInstance(this).registerReceiver(mLoadedReceiver, loadedFilter);

    registerReceiver(mMessageReceiver,new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));

}

PROBLEM

the broadcast instance mMessageReceiver (the 1st line) isn't been registered since dialog(Toast) that are supposed to be activated in its onReceive method aren't been activated. this instance should receive GCM messages that why i have registed it like this ` registerReceiver(mMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));

Question

Where am going wrong ? i have tried to follow the Try Cloud Messaging for Android and even the example at gitlab but all in vain. my previous question relation to this issue is here.

Community
  • 1
  • 1
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74

1 Answers1

0

You need to declare a few permissions, services and GCMReceiver inside the manifest in order for GCM to work as intended.

Different page in the official documentation addresses GCM set up on an Android client in more depth. (refer here and sample here)

Hope this helped.

Kay_N
  • 987
  • 5
  • 12
  • it was a problem with my backend implementation. when i install my app on a device for the first time, am able to receive gcm notifications very well. but when i install for the second tyme, thats when i was not receiving notifications. but i solved the problem. thanks for your concern. – Edijae Crusar Nov 14 '15 at 04:45