在前台应用程序未收到GCM通知

问题描述:

我试图按official reference中所述将推送通知设置为包含数据和通知负载的应用程序。我希望两个有效载荷都能在我的应用程序处于后台时获得自动通知,并且应用程序可以在前台处理自己的事情。在前台应用程序未收到GCM通知

当我的GcmListenerService中的onMessageReceived()方法在包含两个有效载荷且应用程序位于前台时未被调用。我必须忽略通知有效负载才能使用。

当应用程序在后台,它工作正常。我收到通知,可以在点击通知时打开应用程序,并从那里执行任何我想要的操作。我看过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); 
} 

Server代码:

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) 

是的,你可以发送邮件说都有notification and data payloads

我发现这个Stack overflow ticket与您的查询有关,它表示,意向过滤器将永远触发你的显式应用程序按照标准的android行为。确保您的逻辑流程能够处理您的启动可能来自地方的事实。尝试执行'GcmMessageHandler'您的应用程序必须检查保存的状态,并查看用户是否需要查看屏幕并自动显示通知。

我对样本有一些奇怪的行为,但最终我解决了通过删除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> 
+0

我怕我不明白如何完成这个例子。我收到有关“无法解析目标意向服务”的错误。看来我应该设置一些服务来取代我的GcmListenerService? – ArkF

+0

在我的情况下,我完全删除MyGcmListenerService frome代码和清单,它工作正常。但试着用GcmListenerService来构建你的项目,也许它会澄清一些事情。并请提供更多关于错误的信息 – vonShtirlitz