2016-05-13 37 views
1

我有一个广播接收器,它在应用程序处于打开状态并且在后台时检测即将发出的通知,但当最近的应用程序被清除时,接收器无法正常工作,请给我建议。当应用程序关闭时,GCM广播接收器不会检测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); 
    } 
} 

清单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接收消息。

谷歌示例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); 
相关问题