正如这个
Github问题中所解释的,这正是您的问题:
来自https://developers.google.com/cloud-
messaging/server#notifications_and_data_messages “ GCM将代表客户端应用程序显示通知部分。
提供可选数据后,一旦用户单击通知,它将发送到客户端应用程序并打开客户端应用程序。[…]在Android上,数据有效载荷可以在用于启动你的活动的意图取回。
“
因此,在 用户点击通知后 ,数据便以用于启动活动的意图传递。这意味着您需要执行以下操作:
- 将click_action添加到您从服务器发送的通知键中:例如
send_queue.append({'to': REGISTRATION_ID, 'message_id': random_id(), "notification" : {"body" : "Hello from Server! What is going on? Seems to work!!!","title" : "Hello from Server!","icon" : "@drawable/ic_school_white_48dp","sound": "default","color": "#03A9F4","click_action": "OPEN_MAIN_ACTIVITY" }, 'data': { 'message': "Hello" }})有关通知有效负载的信息,请参见以下网址:https :
//developers.google.com/cloud-messaging/server-ref#notification-payload-
support
- 在
AndroidManifest.xml
用户单击通知后要在要打开的活动上添加意图过滤器时,使用与您在服务器端的“ click_action”键上使用的动作名称相同的动作名称,例如:
<activity android:name=".ui.MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="OPEN_MAIN_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 得到的意图的数据在你 的onCreate() 方法 或 在 onNewIntent() ,如果你已经设置的launchMode到singleTop的活动要启动单击该通知时,如:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.hasExtra(Constants.KEY_MESSAGE_TXT)) { String message = intent.getStringExtra(Constants.KEY_MESSAGE_TXT); Log.d(TAG, message); } }我已经对此进行了测试,可以确认它是否有效。(使用XMPP连接)



