0

i want use GCM in my android project.

my codes is:

public class GcmReceiver extends WakefulBroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
      GcmMessageHandler.class.getName());

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));


    setResultCode(Activity.RESULT_OK);
  }
}

and my InstentService is:

public class GcmMessageHandler extends IntentService {

  private Handler handler;

  public GcmMessageHandler() {
    super("GcmMessageHandler");
  }

   @Override
   public void onCreate() {
     super.onCreate();
   }

  @Override
  protected void onHandleIntent(Intent intent) {
  Bundle extra = intent.getExtras();

  showToast();

  GcmReceiverOld.completeWakefulIntent(intent);
 }


  private void showToast()
  {
  handler.post(new Runnable() {
   @Override
   public void run() {
     Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
   }
    });
  }

}

also i added Wale_Lock permission to manifest:

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

and added this line to manifest too

<receiver android:name=".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="ir.test.gcmtester" />
                    </intent-filter>
        </receiver>

        <service android:name=".gcm.GcmMessageHandler"
                 android:exported="false"
            />

but my problem is that when the app full closed (in android 6) the GcmReceiver Class not trigger.

how can solve this problem?

Mahdi Radi
  • 429
  • 2
  • 10
  • 30

0 Answers0