2016-05-13 6 views
1

У меня есть широковещательный приемник, который обнаруживает встречные уведомления, когда приложение открыто и в фоновом режиме, но когда последнее приложение очищено, приемник не работает, пожалуйста, предложите мне идеи.GCM Broadcast приемник не обнаруживает gcm-уведомление при закрытии приложения

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> 
+1

Опубликовать свой манифест. просто хотите проверить, как вы определили получателя – Krishna

+0

Какая версия GCM? Вы должны использовать GcmListenerService вместо BroadcastReceiver –

+0

@krishna, я добавил манифест – Tharun

ответ

0

Попробуйте зарегистрировать приемник в манифесте, используйте GcmListenerService для приема сообщений.

Пример Google https://github.com/googlesamples/google-services имеет пример для этого.

<!-- 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> 

// Используйте эту услугу, чтобы получить сообщение

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()); 
} 

}

0

Когда приложение близко вы должны проснуться приложение вверх, означает, что вы должны иметь разрешение, чтобы сделать это

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

и после того, как вы можете установить:

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     manager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, yourIntent); 
Смежные вопросы