应用程序处于前台时,GCM通知未收到

时间:2016-02-27 17:57:33

标签: android push-notification google-cloud-messaging

我正在尝试设置包含数据和通知有效负载的推送通知,如official reference中所述。我想要两个有效负载,这样当我的应用程序在后台时我会收到自动通知,并且应用程序可以在前台处理事情。

当包含两个有效负载且应用程序位于前台时,我的onMessageReceived()中的GcmListenerService方法未被调用。我必须省去通知有效负载才能工作。

当应用程序在后台时,它可以正常工作。我收到通知,可以在点击通知时打开应用程序,并从那里做任何我想做的事。

我查看了this question,但他们试图在应用程序处于前台时收到通知。我知道在这种情况下我不会收到通知,我只想获取数据。

AndroidManifest.xml的相关部分:

<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="arkenterprises.garage_o_matic.gcm" />
    </intent-filter>
</receiver>
<service
    android:name=".MyGcmListenerService"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

MyGcmListenerService.java:

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.d(TAG, "Data: " + data);
        String message = data.getString("message");
        String doorStatus = data.getString("DoorStatus");
        String time = data.getString("time");
        Bundle notification = data.getBundle("notification");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        Log.d(TAG, "DoorStatus: " + doorStatus);
        Log.d(TAG, "Time: " + time);
        Log.d(TAG, "Notification: " + notification);
}

发送消息的服务器代码:

nowString = datetime.datetime.now().strftime("%c")
gcmSendURL = 'https://gcm-http.googleapis.com/gcm/send'
gcmHeaders = {'Authorization': 'key=' + API_KEY, 'Content-Type': 'application/json'}
gcmDataPayload = {'message': 'Garage door opened', 'DoorStatus': 'Open', 'time': nowString}
gcmNotificationPayload = {'title': 'Garage Door Has Opened', 'body': 'Garage door opened at {}'.format(nowString), 'icon': 'ic_door_open_notification2'}
print('Garage door opened at {}'.format(nowString))
gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'data': gcmDataPayload}
# gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'notification': gcmNotificationPayload}
# gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'content_available': True, 'data': gcmDataPayload, 'notification': gcmNotificationPayload}
print("\nPayload: {}".format(gcmPayload))
r = requests.post(gcmSendURL, headers=gcmHeaders, json=gcmPayload)
print("\nRequest response: " + r.text)

2 个答案:

答案 0 :(得分:0)

是的,您可以发送同时包含notification and data payloads的邮件。

我发现这个Stack overflow ticket与你的查询有关,它说Intent过滤器总会根据标准的android行为激活你的表现应用程序。确保您的逻辑流程能够处理您的发布可能来自某些地方的事实。尝试实施'GcmMessageHandler'您的应用程序必须检查已保存的状态,并查看用户是否需要查看屏幕并自动显示通知。

答案 1 :(得分:0)

我对样本有一些奇怪的行为,但最后我通过删除GcmListenerService的实现并添加了GcmReceiver的实现来解决这个问题。

public class MyGcmReceiver extends GcmReceiver {

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

和清单

<receiver
   android:name=".MyGcmReceiver"
   android:exported="true"
   android:permission="com.google.android.c2dm.permission.SEND" >
   <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
   </intent-filter>
</receiver>