1

I have a broadcast receiver which detects oncoming notifications when the app is open and in background but when the recent app is cleared the receiver is not working please suggest me ideas.

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        JSONObject json = new JSONObject();
        try {
            json.putOpt("userid", StorePreference.GetSharedPreferenceDetails(context, "memberid"));
            json.putOpt("rid",StorePreference.GetSharedPreferenceDetails(context, "partnerid"));
            json.putOpt("message", "Received");
            BoundService.getInstance().onlinestatus(json);
        } catch (Exception e) {
            e.printStackTrace();
        }

        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

Manifest decleration:

<receiver
    android:name="com.twogether.receivers.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
       <action android:name = "com.google.android.c2dm.intent.RECEIVE" />
       <category android:name="com.google.android.gcm.demo.app" />
      </intent-filter>
</receiver>
Tharun
  • 23
  • 8

2 Answers2

0

Try to register your receiver in manifest, use GcmListenerService to receive messages.

google example https://github.com/googlesamples/google-services has example to do this.

    <!-- gcm_receiver, this is android source-->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.myapp.gcm" />
        </intent-filter>
    </receiver>

    <!-- gcm_listener service -->
    <service
        android:name="com.qblinks.qmote.GcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

// Use this service to receive message

public class GcmListenerService extends com.google.android.gms.gcm.GcmListenerService {
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.v(TAG, "From: " + from);
        Log.v(TAG, "Message: " + message);
        sendNotification(message);
}

private void sendNotification(String message) {

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("this is title")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))//multi line
            .setSound(defaultSoundUri); // ring or vibrate if no ring
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(Const.NOTIFICATION_GCM /* ID of notification */, notificationBuilder.build());
}

}

Bubble.C
  • 70
  • 1
  • 9
0

When the app is close you need to wake the app up, means you need to have the permission to do that

<uses-permission android:name="android.permission.WAKE_LOCK" />

and after you can set:

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, yourIntent);
Alexiscanny
  • 581
  • 7
  • 15