【问题标题】:GCM Broadcast receiver not detcting gcm notification when the app is closed当应用程序关闭时,GCM 广播接收器未检测到 gcm 通知
【发布时间】:2016-05-13 09:37:22
【问题描述】:

我有一个广播接收器,它在应用程序打开并在后台检测到即将到来的通知,但是当最近的应用程序被清除时,接收器不工作,请给我建议。

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

清单声明:

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

【问题讨论】:

  • 发布您的清单。只想检查你如何定义接收者
  • 这是什么版本的 GCM?您应该使用 GcmListenerService 而不是 BroadcastReceiver
  • @krishna 我已经添加了清单
  • @TimCastelijns 我需要在后台检测 gcm 通知...

标签: java android google-cloud-messaging broadcastreceiver


【解决方案1】:

尝试在manifest中注册你的receiver,使用GcmListenerService来接收消息。

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

}

【讨论】:

    【解决方案2】:

    当应用关闭时你需要唤醒应用,这意味着你需要有权限这样做

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

    之后可以设置:

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            manager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, yourIntent);
    

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多